【问题标题】:Custom Textview Font not working自定义 Textview 字体不起作用
【发布时间】:2018-03-24 20:19:52
【问题描述】:

我想在 textview 上使用其他字体。我已经创建了自定义textview类并创建了字体文件夹。问题是编译后字体没有改变。我不知道我犯了什么错误。希望你们能帮我解决这个问题。提前谢谢

activity_example_one.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.khoi.androidexample.example.Example_One_Activity">

    <com.khoi.androidexample.custom.TextViewBold
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/texview_bold_id"
        android:text="Hello World!"/>

    <com.khoi.androidexample.custom.TextViewMedium
        android:layout_width="match_parent"
        android:id="@+id/textview_medium_id"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>
</LinearLayout>

Example_One_Activity.java

public class Example_One_Activity extends AppCompatActivity {


    TextViewBold textViewBold;
    TextViewMedium textViewMedium;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_example_one);
        textViewBold = (TextViewBold) findViewById(R.id.texview_bold_id);
        textViewMedium = (TextViewMedium) findViewById(R.id.textview_medium_id);
    }

}

TextViewBold.java

公共类 TextViewBold 扩展 TextView {

public  TextViewBold(Context context, AttributeSet attrs, int defStyle){
    super(context, attrs, defStyle);

    init(context);
}

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

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


private void init(Context context) {
    if (!isInEditMode()) {
        Typeface tf = Typeface.createFromAsset(context.getAssets(),"Lato_Medium.ttf");
        setTypeface(tf);
    }
}

}

【问题讨论】:

    标签: java android fonts textview


    【解决方案1】:

    在你的 init(Context context)

    中传递上下文

    和 getAssets() 一样 --> context.getAssets()

    Typeface tf = Typeface.createFromAsset(context.getAssets(),"Lato_Medium.ttf");
    

    isInEditMode() -->确保它会返回true

    【讨论】:

    • 我已经添加了上下文作为参数并且 !isInEditMode() 返回 true,但是 textview 字体仍然没有改变。
    • 请在没有 if(!isInEditMode()) 的情况下再次检查并尽快更新我
    • 如果 isInEditMode() 只返回 false。
    • 按住 ctrl 键并将鼠标光标移动到“Lato_Medium.ttf”,如果单击该文本,是否会打开文件?请确认我会给出解决方案。
    • 您的代码看起来不错。代码没有问题。检查您的 ttf 文件。检查与其他字体文件相同的代码。请更新您的问题代码。因为你用错了——getResources().getAssets()。所以请更新你的问题代码
    【解决方案2】:

    您的地址中缺少“fonts/”,应该是这样的:

             setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/Lato_Medium.ttf"));
    

    【讨论】:

    • 我收到一条错误消息:原因:java.lang.RuntimeException: Font assets not found fonts/Lato_Medium.ttf
    • 尝试重建项目,然后复制我上面编辑的确切代码,它应该可以工作,我看不出代码中有任何问题
    【解决方案3】:

    我重新下载字体样式并替换字体文件夹中的文件,它工作。非常感谢您帮助解决了这个问题

    【讨论】:

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