2010-04-01 55 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