如标题所示,我为常规,粗体等设置了自定义字体。当我在xml中将字体设置为粗体或斜体时,它将在设计器中显示,但不会出现在实际设备上(三星galaxy s9)。我尝试了几种不同的策略,但要使其正常工作,唯一的方法是通过编程设置字体。
Typeface boldFont = ResourcesCompat.getFont(getContext(), R.font.cogito_bold);
testTextView.setTypeface(boldFont);
这里是textview的xml布局所在的位置
<TextView
android:id="@+id/testText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:layout_weight="4"
android:autoSizeMaxTextSize="44sp"
android:autoSizeMinTextSize="20sp"
android:autoSizeStepGranularity="2sp"
android:autoSizeTextType="uniform"
android:textSize="42sp"
android:fontFamily="@font/cogito_font"
android:textStyle="bold"
android:text="Test Words"
android:textColor="@color/wb_white" />
这是我的自定义字体系列,然后我的.otf文件位于相同的res / font目录中。我也尝试过仅将textview的fontfamily直接设置为cogito_bold.otf,但是没有运气。
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
<font
xmlns:app="http://schemas.android.com/apk/res-auto"
android:font="@font/cogito_regular"
android:fontStyle="normal"
android:fontWeight="400"
app:font="@font/cogito_regular"
app:fontStyle="normal"
app:fontWeight="400" />
<font
xmlns:app="http://schemas.android.com/apk/res-auto"
android:font="@font/cogito_italic"
android:fontStyle="italic"
android:fontWeight="400"
app:font="@font/cogito_italic"
app:fontStyle="italic"
app:fontWeight="400" />
<font
xmlns:app="http://schemas.android.com/apk/res-auto"
android:font="@font/cogito_bold"
android:fontStyle="normal"
android:fontWeight="700"
app:font="@font/cogito_bold"
app:fontStyle="normal"
app:fontWeight="700" />
</font-family>
您可以在不创建字体系列的情况下执行此操作。只需将字体添加到res> font目录。
app_font_bold.ttf
app_font_regular.ttf
并调用XML中的字体。
android:fontFamily="@font/app_font_bold"