0

我正在尝试将默认字体更改为自定义字体(handa.ttf)。

我在路径中找到了文件handa.ttf

MyProject/app/src/main/assets/fonts/handa.ttf

我以编程方式在java中定义了我的字体,如下所示:

Typeface tp1 = Typeface.createFromAsset(getAssets(),"fonts/handa.ttf");
logo1t = (TextView) findViewById(R.id.log1text);
logo2t = (TextView) findViewById(R.id.log2text);
logo2t.setTypeface(tp1);  

我的代码没有错误,但是当我运行我的应用程序时,没有发生任何变化!字体保持默认!

我认为我的 XML 没有错误:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#fffaf0"
    android:gravity="center">
<ImageView
android:id="@+id/mainiconimage"
android:src="@drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="60dp"/> 
<LinearLayout
android:layout_alignTop="@+id/mainiconimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:text="High"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/log1text"
android:textSize="20sp"
android:textColor="#880000ff"/>
<TextView
android:text="Skill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/log2text"
android:padding="4dp"
android:background="@drawable/textbg"
android:textSize="20sp"/>
</LinearLayout>

</LinearLayout> 

怎么了!

4

1 回答 1

0

只需在 res 文件夹中添加一个字体文件夹(示例)。您可以像这样在 xml 文件中设置字体

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/your_font_here"
            android:text="Some text"
            android:textColor="@color/some_color"
            android:textSize="12sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
于 2021-01-17T20:49:46.287 回答