【问题标题】:Typeface custom font doesn't work字体自定义字体不起作用
【发布时间】:2016-12-25 10:42:43
【问题描述】:

字体始终显示默认字体。

我的字体存储在 assets/fonts 中。而且我尝试过使用其他字体,并重新编码字体。 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" />

【问题讨论】:

    标签: java android truetype


    【解决方案1】:

    如果您以前没有尝试过使用字体。我建议删除您的资产文件夹,在您的 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" />
    

    在你的 values 文件夹中创建一个 attrs.xml。

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

    这将使您不必在每次想要在按钮上使用自定义字体(或大多数其他小部件,可以应用相同的逻辑)时都执行setTypeFace

    【讨论】:

    • 好的,资产文件夹没有问题。我认为您建议的第二种方式很棒,但我不明白如何定义 CustomFontButtonCustomFontButton_customFont
    • 在您的 res 文件夹中,右键单击您的 values 文件夹并添加一个新的 xml,将其命名为 attrs.xml 并复制我在答案末尾添加的代码
    • 不,我已经实现了,但没有结果。
    • 日志说什么?你有任何错误吗?将堆栈添加到您的问题。我帮你解决
    • A 也尝试过this,但按钮仍然具有默认字体,没有任何错误。我决定重新设计我的应用程序,为了更好看,我现在正在使用 ImageButons。但无论如何,谢谢你的帮助。
    【解决方案2】:

    不要直接使用setTypeface方法设置字体,试试下面的代码sn-p:

    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;
    }
    

    如果遇到任何问题,请告诉我。

    【讨论】:

      【解决方案3】:

      一个不错的选择是仔细检查您的文件夹、字体和字体目录名称的拼写。一个好主意是显示所有已打印的错误并将它们发布,以便我们更好地理解问题。如果您还可以发布字体名称并仔细检查它们是否相同,并确保您的资产位于正确的位置,希望我能提供帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-04-22
        • 1970-01-01
        • 2015-11-08
        • 2012-10-17
        • 2018-06-07
        相关资源
        最近更新 更多