android:使用 textAppearance 属性设置 textColor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11225851/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:24:18  来源:igfitidea点击:

android: set textColor with textAppearance attribute

androidtext

提问by Eugene Chumak

I have created several styles for font in my app.

我在我的应用程序中为字体创建了几种样式。

How can I add these styles to views? - 1) Using style attribute 2) Using textAppearance.

如何将这些样式添加到视图中?- 1) 使用样式属性 2) 使用 textAppearance。

To use style is not an option because views may have other attributes (margins, paddings, min width, etc - I cant specify 2 styles for a view), so I need to specify text-related attributes separately, but android:textColordoesnt work here:

使用样式不是一种选择,因为视图可能具有其他属性(边距、填充、最小宽度等 - 我无法为视图指定 2 种样式),因此我需要单独指定与文本相关的属性,但android:textColor在这里不起作用:

styles.xml:

样式.xml:

<style name="TextAppearance.baseText" parent="@android:style/TextAppearance">
    <item name="android:textAppearance">?android:textAppearanceSmall</item>
    <item name="android:typeface">sans</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">15sp</item> 
</style>

layout.xml:

布局.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="65dp"
      android:orientation="horizontal" >

      <Button
           android:id="@+id/backButton"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/sometext"
           android:textAppearance="@style/TextAppearance.baseText"/>
</RelativeLayout>

Why doesnt it work? How can I specify textcolor in textappearance?

为什么它不起作用?如何在 textappearance 中指定 textcolor?

回答by TheIT

As Oderik has mentioned in the comments, the Button view has a default value for it's textColorattribute. This textColorvalue overrides whatever value is set through the textAppearanceattribute. Therefore you have two options:

正如 Oderik 在评论中提到的, Button 视图的textColor属性有一个默认值。此textColor值会覆盖通过textAppearance属性设置的任何值。因此,您有两个选择:

  • Set the textColor to @null in the style:

    <style name="Application.Button">   
      <item name="android:textAppearance">@style/Application.Text.Medium.White</item>
      <item name="android:textColor">@null</item> 
    </style>
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/Application.Button" />
    
  • Set the textColor to @null in the Button XML:

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/Application.Button"   
    android:textColor="@null" />
    
  • 在样式中将 textColor 设置为 @null:

    <style name="Application.Button">   
      <item name="android:textAppearance">@style/Application.Text.Medium.White</item>
      <item name="android:textColor">@null</item> 
    </style>
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/Application.Button" />
    
  • 在 Button XML 中将 textColor 设置为 @null:

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/Application.Button"   
    android:textColor="@null" />
    

回答by ania

It works. Your text color is white - the same as default color. Put there any other color like <item name="android:textColor">#AA6699</item>and you will see difference. Second thing is that you are trying to set text appearance in text appearance - doen's make sense. If you want to set small letters do this using parent parent="@android:style/TextAppearance.Smalland delete line <item name="android:textAppearance">?android:textAppearanceSmall</item>Third thing is that you want to have small letters and put additional textSize - doesn't make sense. Try this, works for me:

有用。您的文本颜色为白色 - 与默认颜色相同。把任何其他颜色放在那里<item name="android:textColor">#AA6699</item>,你会看到不同。第二件事是您试图在文本外观中设置文本外观 - 确实有意义。如果您想设置小写字母,请使用 parentparent="@android:style/TextAppearance.Small和 delete line执行此操作<item name="android:textAppearance">?android:textAppearanceSmall</item>第三件事是您想要小写字母并添加额外的 textSize - 没有意义。试试这个,对我有用:

<style name="BaseText" parent="@android:style/TextAppearance.Small">
        <item name="android:typeface">sans</item>
        <item name="android:textColor">#AA7755</item>
  </style>

If you want to set everything in style:

如果您想设置所有样式:

<style name="BaseText" parent="@android:style/TextAppearance.Large">
        <item name="android:typeface">sans</item>
        <item name="android:textColor">#AA7755</item>
    </style>

   <style name="BaseText.Margins">
       <item name="android:layout_margin">10dip</item>
   </style>

Where BaseText.Margins inherits from BaseText. Then you can just use:

其中 BaseText.Margins 继承自 BaseText。然后你可以使用:

<TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        style="@style/BaseText.Margins" />

回答by ZakTaccardi

If you want to use android:TextAppearanceinstead of style:to set your android:textColor, you need to set android:textColor=@nullon your TextView.

如果要使用android:TextAppearance而不是style:设置您的android:textColor,则需要android:textColor=@null在您的TextView.

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:textAppearance="@style/TextAppearance.AppCompat.Medium.MySubheader"
    android:textColor="@null"
    tools:text="Section" />

Then it will inherit correctly from your android:TextAppearance

然后它将从您的正确继承 android:TextAppearance

<style name="TextAppearance.AppCompat.Medium.MySubheader">
    <item name="android:fontFamily">sans-serif-medium</item>
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@color/black_54</item>
</style>