【问题标题】:TextAppearance override style's fontFamilyTextAppearance 覆盖样式的 fontFamily
【发布时间】:2021-04-02 09:56:28
【问题描述】:

我有一个带有自定义 fontFamily 的 TextView 样式。另外,我的 textAppearance 有一个单独的样式。

当我将我的 TextView 样式和 textAppearance 样式设置为单个 TextView 时,fontFamily 被忽略了。但是当样式设置没有 textAppearace 时一切正常。

样式是使用 airbnb 的 Paris 库设置的。

TextView 样式:

<style name="TextViewBold" parent="Widget.MaterialComponents.TextView">
    <item name="fontFamily">@font/roboto_bold</item>
    <item name="android:fontFamily">@font/roboto_bold</item>
    <item name="android:textColor">@color/black</item>
</style>

TextAppearance 样式:

<style name="TextAppearanceAllCaps">
    <item name="textAllCaps">true</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:textColor">@color/anotherColor</item>
</style>

以编程方式设置样式:

MaterialTextView(context!!).apply {
        style(R.style.TextViewBold) //Paris used here
        updateTextAppearance(R.style.TextAppearanceAllCaps) //custom extension
        setText(R.string.some_text)
}

扩展代码:

fun TextView.updateTextAppearance(@StyleRes resId: Int) {
    TextViewCompat.setTextAppearance(this, resId)
}

如何解决此问题?

【问题讨论】:

  • 将你的 textAppearance 样式作为一个 item 放入 Textview 样式中
  • @D_K 我不能。因为这种风格是几个 TextView 的基础。使用 textAppearance 我可以为每个 TextView 调整一些特定的东西
  • 你可以手动设置textAllCaps和textcolor,所以只要设置属性就行了
  • @D_K 我同意你的观点,但我不想对每个视图都执行这些重复操作。此外,如果样式发生一些变化,单个地方将被重构

标签: android textview styles material-components-android


【解决方案1】:

只需从定义了fontFamily 的父级继承TextAppearance,或将其添加到您的自定义TextAppearance 中。

例如:

<style name="TextViewBold" parent="Widget.MaterialComponents.TextView">
   <item name="android:textAppearance">TextAppearance.App</item>
</style>


<style name="TextAppearance.App" parent="TextAppearance.MaterialComponents.Body1">
   <item name="fontFamily">@font/roboto_bold</item>
   <!-- .... -->
</style>

<!-- Inherit from TextAppearance.App -->
<style name="TextAppearance.App.AllCaps"> 
   <item name="textAllCaps">true</item>
</style>

然后将样式@style/TextViewBold 应用到TextView 并使用style/TextAppearance.App.AllCaps 更新textAppearance

【讨论】:

  • 这改变了文本的外观,但fontFamily 仍然被忽略(
  • @Skullper 刚试过,没问题。
  • 哦,对不起!你是对的,它有效。但我不能使用这种方法。我以基本样式声明了fontFamily。这种风格在不同的 TextViews 中。我只是不想在textAppearance 中复制fontFamily,因为它不应该处理它。它应该只使用在视图样式中声明的fontFamily
  • 你不需要复制它。只需添加样式 ....。答案已更新。
  • 非常感谢,加布里埃尔。这种方法不适合我的应用架构,但对其他人来说,它会很有用
【解决方案2】:

你可以这样做:

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textAllCaps="true"
    android:textColor="@color/white"
    android:theme="your theme"
    />

【讨论】:

  • 如您所见,我仅在代码中创建视图。我没有使用任何 xml
  • 看到这个答案希望这能解决你的答案让我知道它是否会stackoverflow.com/a/17896729/12543430
【解决方案3】:

我建议使用此工作流程来设置文本样式:

  1. 在你的主题的 textViewStyle 默认样式中设置任何应用范围的样式。

  2. 设置您的应用将使用的(或使用/扩展自 MaterialComponent 的 styles)的(少量)TextAppearances,并直接从您的视图中引用这些内容

  3. 创建样式设置 TextAppearance 不支持的任何属性(它们本身指定您的 TextAppearances 之一)。

  4. 直接在您的布局中执行任何独特的样式。

【讨论】:

  • 此时无法在app主题中设置textViewStyle
  • 然后尝试从父字体系列继承。
【解决方案4】:

感谢所有愿意提供帮助的人!

我发现了一个问题和一个解决方案(针对我的具体情况)。在大多数情况下,您可以检查接受的答案。

问题:

textAppearance 以编程方式设置时,样式fontFamily 属性将被删除。我什至尝试设置我的文本外观有一些延迟,以检查 UI 线程是否没有过载,但它没有帮助。 另外,我尝试在没有巴黎的情况下设置风格,也没有帮助。

顺便说一句,我没找到原因:(

解决方案:

我只用我的TextView 元素创建了 XML 文件(感谢@D_K 的提示)。然后我在 XML 中设置样式和文本外观,并在需要的地方对其进行扩充。 只有使用这种方法,我才能实现我想要的。

附:如果你对文本颜色有问题,只需在文本视图样式中将其设置为@null,然后将使用来自 textAppearance 的textColor

【讨论】:

    猜你喜欢
    • 2018-01-15
    • 1970-01-01
    • 2013-10-18
    • 2020-12-20
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 2020-09-21
    相关资源
    最近更新 更多