2016-04-25 45 views
0

我想更改完整應用的字體! 但我不知道如何訪問完整的應用程序。 我有一種方法可以通過Id訪問文本,如何更改代碼以訪問整個應用程序?完整應用的Android外部字體

public class ExternalFont extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    String fontPath = "fonts/FreeUniversal-Regular.ttf"; 
    TextView txtUniversal = (TextView) findViewById(R.id.universal); 
    Typeface tf = Typeface.createFromAsset(getAssets(), fontPath); 
    txtUniversal.setTypeface(tf); 
} 

}

回答

0

創建您自己的TextView與自定義樣式。

例子:

public class YourTextView extends TextView { 

    public YourTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public YourTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(); 
    } 

    public YourTextView(Context context) { 
     super(context); 
     init(); 
    } 

    private void init() { 
     Typeface tf = Typeface.createFromAsset(context.getAssets(), 
      "fonts/yourfont.ttf"); 
     setTypeface(tf); 
    } 
} 

你可以在你的XML使用。

0

不幸的是,沒有直接的方法可以通過更改默認字體來更改您在應用中使用的所有TextView的字體。

你可以做的是創建自己的自定義TextView並按照#josedlujan所建議的設置字體。但是這種方法的缺陷是每次創建一個TextView對象時,都會創建一個新的Typeface對象,這對RAM的使用來說是非常糟糕的(對於像Roboto這樣的字體,字體對象通常會在8-13 kb之間變化)。所以最好在你的字體被加載後緩存。

步驟1:創建爲您的應用字樣緩存 -

public enum Font { 

    // path to your font asset 
    Y("fonts/Roboto-Regular.ttf"); 

    public final String name; 
    private Typeface typeface; 

    Font(String s) { 
    name = s; 
    } 

    public Typeface getTypeface(Context context) { 

    if (typeface != null) return typeface; 
    try { 
     return typeface = Typeface.createFromAsset(context.getAssets(), name); 
    } catch (Exception e) { 
     return null; 
    } 
    } 

    public static Font fromName(String fontName) { 

    return Font.valueOf(fontName.toUpperCase()); 
    } 

第2步:在attrs.xml定義自己的自定義屬性 「CustomFont的」 爲您CustomTextView

attrs.xml - 
<declare-styleable name="CustomFontTextView"> 
    <attr name="customFont"/> 
</declare-styleable> 


<com.packagename.CustomFontTextView 
      android:id="@+id/some_id" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      app:customFont="rl"/> 

第3步:創建自己的自定義TextView

public class CustomFontTextView extends TextView { 

public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    init(context, attrs); 
} 

public CustomFontTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context, attrs); 
} 

public CustomFontTextView(Context context) { 
    super(context); 
    init(context, null); 
} 

private void init(Context context, AttributeSet attrs) { 

    if (attrs == null) { 
    return; 
    } 

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView, 0 ,0); 
    String fontName = a.getString(R.styleable.CustomFontTextView_customFont); 

    String textStyle = attrs.getAttributeValue(styleScheme, styleAttribute); 

    if (fontName != null) { 
    Typeface typeface = Font.fromName(fontName).getTypeface(context); 
    if (textStyle != null) { 
     int style = Integer.decode(textStyle); 
     setTypeface(typeface, style); 
    } else { 
     setTypeface(typeface); 
    } 
    } 
    a.recycle(); 
}