2016-12-24 83 views
1

字體始終顯示默認字體。字體自定義字體不起作用

我的字體都存儲在資產/字體。我試圖使用其他字體,並重新編碼字體。 PixlUI庫也沒有解決問題。

MainActivity.java

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(R.layout.activity_main); 

    Button button = (Button) findViewById(R.id.button); 
    Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/OldEnglishFive.ttf"); 
    button.setTypeface(typeface); 
} 

activity_main.xml中

<Button 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="220dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:onClick="doSomething" 
    android:text="TEXT" /> 

回答

0

如果您尚未使用的字體試過前面。我會建議刪除您的資產文件夾,在res文件夾中創建一個新的資產文件夾,然後將其移回原來的位置。有時Android Studio不接受構建的資產文件夾。

如下所示Runtime Exception: Font not found for every font i've tried - Android

還,我會建議創建一個擴展Button,以便它會自動將您爲小部件的字體,如果您分配正確的XML到按鈕的類。

一個例子是:

public class CustomFontButton extends Button { 
    AttributeSet attr; 
    public CustomFontButton(Context context) { 
     super(context); 
     setCustomFont(context, attr); 
    } 

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

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

    private void setCustomFont(Context ctx, AttributeSet attrs) { 
     String customFont = null; 
     TypedArray a = null; 
     if (attrs != null) { 
      a = ctx.obtainStyledAttributes(attrs, R.styleable.CustomFontButton); 
      customFont = a.getString(R.styleable.CustomFontButton_customFont); 
     } 
     if (customFont == null) customFont = "fonts/OldEnglishFive.ttf"; 
     setCustomFont(ctx, customFont); 
     if (a != null) { 
      a.recycle(); 
     } 
    } 

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

,然後在你的XML你只需要改變

<yourpackagename.CustomFontButton 
    android:id="@+id/button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="220dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:onClick="doSomething" 
    android:text="TEXT" /> 

創建自己的價值觀的文件夾內的attrs.xml。

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

這將節省你不必做setTypeFace每次你想上的按鈕自定義字體的使用setTypeface方法直接設置字樣(或大多數其他窗口小部件,相同的邏輯可以應用)

+0

好的,資產** _文件夾_ **沒有問題。 我認爲你提出的第二種方法很棒,但我不明白如何定義_CustomFontButton_和_CustomFontButton_customFont_。 –

+0

在您的res文件夾中,右鍵單擊您的值文件夾並添加一個新的xml,將其稱爲attrs.xml並複製我在答案末尾添加的代碼 –

+0

不,我實現了它,但沒有結果。 –

0

相反,請嘗試下面的代碼片段:

protected void onCreate(Bundle savedInstanceState) { 
    ... //do whatever you want 

    Button button = (Button) findViewById(R.id.button); 
    Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/OldEnglishFive.ttf"); 

    button.setText(changeFontStyle("your text", typeface)); 
} 

// custom function for changing the font style of text 
public Spannable changeFontStyle(String finalString, Typeface typeface){ 

    spannable = new SpannableString(finalString); 
    spannable.setSpan(new CustomTypefaceSpan("", typeface), 0, finalString.length(), 0); 

    return spannable; 
} 

我知道是否面臨任何問題。

0

一個不錯的選擇是仔細檢查文件夾,字體和字體目錄名拼寫。一個好主意是顯示任何錯誤,這些錯誤將被打印併發布給我們,以便更好地理解問題。如果您還可以發佈字體名稱並仔細檢查它們是否相同,並確保您的資產位於正確的位置,希望我能幫到您。