2010-04-01 98 views
26

我正在为我的android应用定义样式XML。我有一些我想使用的TTF文件,我如何设置字体使用这些文件作为字体,而不是通用的“sans”,“serif”&“monospace”。谢谢在styles.xml中设置特定字体

回答

47

您只能通过Java代码使用自定义字体,而不能通过布局XML或样式/主题 - 抱歉!

+0

非常感谢我很感激。 – NickTFried 2010-04-02 14:34:51

+0

@SergiCastellsaguéMillán:这个问题是为了使用TTF字体。你链接到的答案是任何**,但** TTF字体。 – CommonsWare 2014-01-05 22:26:23

+1

由于这是一个旧的答案,你知道这是否有更新的版本,@CommonsWare?我也希望能够通过在''文档中定义字体来更改字体。 – 2015-08-25 18:32:37

7
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="CodeFont" parent="@android:style/TextAppearance.Medium"> 
     <item name="android:layout_width">fill_parent</item> 
     <item name="android:layout_height">wrap_content</item> 
     <item name="android:textColor">#00FF00</item> 
     <item name="android:typeface">monospace</item> 
    </style> 
</resources> 
+2

什么是monspace这里?如果我有.ttf文件,该怎么办? – sheetal 2015-07-09 18:49:32

+1

https://en.wikipedia.org/wiki/Monospaced_font是字体类型 – 2015-10-19 12:38:32

+1

如果我想使用sans-serif,该怎么办? – Senhor 2016-07-21 21:02:04

16

TextViewPlus.java:

package com.example; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Typeface; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.widget.TextView; 

public class TextViewPlus extends TextView { 
    private static final String TAG = "TextView"; 

    public TextViewPlus(Context context) { 
     super(context); 
    } 

    public TextViewPlus(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setCustomFont(context, attrs); 
    } 

    public TextViewPlus(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setCustomFont(context, attrs); 
    } 

    private void setCustomFont(Context ctx, AttributeSet attrs) { 
     TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus); 
     String customFont = a.getString(R.styleable.TextViewPlus_customFont); 
     setCustomFont(ctx, customFont); 
     a.recycle(); 
    } 

    public boolean setCustomFont(Context ctx, String asset) { 
     Typeface tf = null; 
     try { 
     tf = Typeface.createFromAsset(ctx.getAssets(), asset); 
     } catch (Exception e) { 
      Log.e(TAG, "Could not get typeface: "+e.getMessage()); 
      return false; 
     } 

     setTypeface(tf); 
     return true; 
    } 

} 

attrs.xml:(在res /值)

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="TextViewPlus"> 
     <attr name="customFont" format="string"/> 
    </declare-styleable> 
</resources> 

的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:foo="http://schemas.android.com/apk/res/com.example" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <com.example.TextViewPlus 
     android:id="@+id/textViewPlus1" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:text="@string/showingOffTheNewTypeface" 
     foo:customFont="saxmono.ttf"> 
    </com.example.TextViewPlus> 
</LinearLayout> 

你会把 “saxmono.ttf” 在资产文件夹。

0

是的,你可以:)

第1步:创建一个文件夹并将其命名为“字体”,并在它里面你的.ttf。 enter image description here

第2步:进入style.xml并执行以下操作: - enter image description here

第3步:在任何UI对象使用的样式标签: -

enter image description here

+0

在此之前,首先,您需要更新以更新以支持库26,这里是链接https://segunfamisa.com/posts/custom-fonts-with-android-support-library – 2018-01-12 18:19:28