android:使用textAppearance属性设置textColor

Eug*_*mak 5 android text

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

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

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

styles.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>
Run Code Online (Sandbox Code Playgroud)

layout.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>
Run Code Online (Sandbox Code Playgroud)

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

The*_*eIT 11

正如Oderik在评论中提到的,Button视图具有其textColor属性的默认值.此textColor值将覆盖通过该textAppearance属性设置的任何值.因此,您有两种选择:

  • 在样式中将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" />
    
    Run Code Online (Sandbox Code Playgroud)
  • 在Button XML中将textColor设置为@null:

    <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/Application.Button"   
    android:textColor="@null" />
    
    Run Code Online (Sandbox Code Playgroud)


ani*_*nia 8

有用.您的文字颜色为白色 - 与默认颜色相同.放在那里任何其他颜色<item name="android:textColor">#AA6699</item>,你会看到差异.第二件事是你试图在文本外观中设置文本外观 - doen是有意义的.如果你想设置小写字母使用父parent="@android:style/TextAppearance.Small和删除行这样做<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>
Run Code Online (Sandbox Code Playgroud)

如果你想设置风格的一切:

<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>
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

  • 默认的Button样式显式设置文本颜色,其优先级高于通过textAppearance设置的文本颜色.要避免这种情况,请通过将属性设置为"@ null"来修改按钮样式以不使用显式文本颜色. (8认同)

Zak*_*rdi 7

如果要使用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" />
Run Code Online (Sandbox Code Playgroud)

然后它将从您的正确继承 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>
Run Code Online (Sandbox Code Playgroud)