我正在尝试使用支持库(适用于Android> 6)在Android应用中设置自定义字体,出于某种原因,似乎只有TextView
个字体可以使用它们。
这是布局的代码:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
style="@style/ItemPrefs"
android:text="this is textview"
android:fontFamily="@font/questrial" />
<Switch
android:id="@+id/swcCustomSwitch"
style="@style/ItemPrefs"
android:text="this is switch"
android:fontFamily="@font/questrial" />
</LinearLayout>
</ScrollView>
这是字体文件的代码:
<font-family
xmlns:app="http://schemas.android.com/apk/res-auto">
<font
app:fontStyle="normal"
app:fontWeight="400"
app:font="@font/questrial_regular" />
</font-family>
这就是我得到的:
TextView
,带有自定义字体,Switch
,带有标准字体。
但是,如果我以编程方式进行设置...
{
Typeface customFont = ResourcesCompat.getFont(getContext(), R.font.questrial);
swcCustomSwitch.setTypeface(customFont);
}
...然后一切正常...
我缺少任何“兼容”的东西吗?我不想为每种布局中的每个组件指定字体。
谢谢。
答案 0 :(得分:1)
创建您自己的Switch组件所需的全部只是从父级扩展而已。在initialize方法内部,添加几行代码,这些代码将从xml参数中获取fontFamily并将其应用于文本标题。我认为您可以在TextView代码内部看到相同的内容。