Nav*_*rab 11 android android-fonts android-theme android-styles androidx
很奇怪为什么 fontFamily 不能在 androidX 上工作。
这是我的代码:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="testing "
android:textSize="17sp"
android:fontFamily="@font/iransans"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Run Code Online (Sandbox Code Playgroud)
上面的代码根本不影响字体,当我将 textview 转换为 app compact 时,它可以工作:
<androidx.appcompat.widget.AppCompatTextView
Run Code Online (Sandbox Code Playgroud)
我想设置我的整个应用程序字体,但它不像我预期的那样工作:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:fontFamily">@font/iransans</item>
</style>
Run Code Online (Sandbox Code Playgroud)
有什么办法可以解决这个问题吗?如何在不为每个视图设置字体系列的情况下在整个应用程序中使用我的字体?
Bar*_*owe 25
您可能会遇到 1+ 3 件事:
android:fontFamily但未设置fontFamily主题。确保你同时拥有:<item name="fontFamily">@font/iransans</item>
<item name="android:fontFamily">@font/iransans</item>
Run Code Online (Sandbox Code Playgroud)
如果您在较旧的 API 级别上运行,则可能会遇到此错误,升级到 androidx.appcompat 依赖项可能会解决问题:
https://developer.android.com/jetpack/androidx/releases/appcompat#1.1.0-alpha02
如果您在 中定义了任何其他主题styles.xml,并且您的父TextView主题使用单独的主题,则该主题可能优先fontFamily于视图上的属性(我尚未确定原因,但这是我正在运行的场景进入我自己)。将android:thememyTextView上的直接设置为定义字体系列的样式即可解决问题。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/MyCustomFontTextAppearance" />
Run Code Online (Sandbox Code Playgroud)
在styles.xml中:
<style name="MyCustomFontTextAppearance" parent="TextAppearance.AppCompat.Body1">
<item name="fontFamily">@font/iransans</item>
<item name="android:fontFamily">@font/iransans</item>
</style>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6394 次 |
| 最近记录: |