我的样式文件无法设置自定义字体。
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:textViewStyle">@style/fontTextView</item>
...
</style>
<style name="fontTextView" parent="@android:style/Widget.TextView">
<item name="android:fontFamily">@font/example</item>
</style>
</resources>
我在我的应用程序中仍然看到默认字体。但是当我将fontFamily
更改为其中一个默认字体时,它就会改变。
<style name="fontTextView" parent="@android:style/Widget.TextView">
<item name="android:fontFamily">monospace</item>
</style>
如果我通过编程设置自定义字体,它也会起作用。
setTypeface(ResourcesCompat.getFont(context, R.font.example));
如何在
styles.xml
中使用自定义字体?我将
compiltSdkVersion
设置为26
,并将buildToolsVersion
设置为26.0.2
。同时,我正在使用支持库: def supportVersion = '26.1.0'
implementation "com.android.support:appcompat-v7:$supportVersion"
我的字体系列:
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/example_regular"
app:fontStyle="normal"
app:fontWeight="300"
app:font="@font/example_regular"/>
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/example_regular"
app:fontStyle="normal"
app:fontWeight="400"
app:font="@font/example_regular"/>
<font
android:fontStyle="normal"
android:fontWeight="400"
android:font="@font/example_regular"
app:fontStyle="normal"
app:fontWeight="700"
app:font="@font/example_regular"/>
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="@font/example_regular"
app:fontStyle="italic"
app:fontWeight="300"
app:font="@font/example_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="@font/example_regular"
app:fontStyle="italic"
app:fontWeight="400"
app:font="@font/example_regular" />
<font
android:fontStyle="italic"
android:fontWeight="400"
android:font="@font/example_regular"
app:fontStyle="italic"
app:fontWeight="700"
app:font="@font/example_regular" />
</font-family>
我用同一个*ttf
文件进行测试。
setTypeface(ResourcesCompat.getFont(context, R.font.example));
- Lau