我无法在我的样式文件中设置自定义字体。
<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
文件进行测试。
最佳答案
好的。我解决了这个问题 - 这种行为的原因很简单......我正在扩展 Activity
而不是 AppCompatActivity
关于styles.xml 中的 Android Studio 3.0 自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46905310/