Android:如何设置应用程序范围的默认字体,而不覆盖 textAppearance

标签 android android-theme android-styles android-fonts

我在我的 Android 应用程序中使用自定义字体。我想将此自定义字体设置为应用程序默认字体(作为回退),但我仍然想使用 TextView textAppearance 属性来设置字体和文本样式。

它看起来像在我的应用程序基本主题中设置 textViewStylefontFamily,allays 覆盖 TextView textAppearance 样式?

<style name="MyApp.Base" parent="Theme.AppCompat.Light.NoActionBar">

    <!-- Overriding fontFamily also overrides textView textAppearance -->
    <item name="android:fontFamily">@font/open_sans</item>

    <!-- Overriding textViewStyle also overrides textView textAppearance -->
    <item name="android:textViewStyle">@style/DefaultTextAppearance</item>

</style>

 <style name="DefaultTextAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textStyle">normal</item>
    <item name="fontFamily">@font/open_sans_regular</item>
</style>

<style name="BoldTextAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textStyle">normal</item>
    <item name="fontFamily">@font/open_sans_extra_bold</item>
</style>


<!-- This textView does not get bold style -->
<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Me happy"
  android:textAppearance="@style/BoldTextAppearance" />

最佳答案

View 的样式属性优先于 textAppearance。

在这两种情况下,您在样式中都有 fontFamily 而不是 textAppearance。当您将 fontFamily 直接放在基本主题中时, View 会从其 Activity 中获取该样式。

因此,您需要设置基本 TextView 的 textAppearance,而不是在样式中设置基本 fontFamily 值:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:textViewStyle">@style/TextViewStyle</item>
</style>

<style name="TextViewStyle" parent="@android:style/TextAppearance.Widget.TextView">
    <item name="android:textAppearance">@style/DefaultTextAppearance</item>
</style>

<style name="DefaultTextAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textStyle">normal</item>
    <item name="fontFamily">@font/roboto_regular</item>
</style>

<style name="LobsterTextAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textStyle">normal</item>
    <item name="fontFamily">@font/lobster</item>
</style>

所以,这里我有 2 个 textAppearance:DefaultTextAppearance,用作所有 TextView 的默认值和 LobsterTextAppearance,我在特定情况下使用。

布局看起来像:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Default roboto text" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Lobster text"
    android:textAppearance="@style/LobsterTextAppearance" />

因此,第一个 TextView 使用来自基本 textAppearance 的 fontFamily,第二个使用覆盖的 @style/LobsterTextAppearance

fontFamily textAppearance preview

关于Android:如何设置应用程序范围的默认字体,而不覆盖 textAppearance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47823085/

相关文章:

从 R.attr 引用返回颜色资源 ID 的 Android 测试方法

android - 网页出现在ipad屏幕的一半

java - Android - 在数组中创建 JSON 数组

java - StringBuilder.toString 是否保留构建的字符串?

android - 更改 list 中的主题不起作用

android - 设置 ShareActionProvider 的颜色

android - 具有旧样式和不同颜色的 TimePickerDialog

Android,如何让应用程序的任务不可关闭?只能通过任务终止来关闭

android - "Possible overdraw: Root element paints background "

android - 无法在我的应用程序的代码中设置主题