我想在我的 AppTheme 中设置一个默认的文本颜色,它应该是黑色的(不是默认的 Material Design 深灰色)。应通过在 UI 元素(例如 TextView)上通过 android:textAppearance 属性设置自定义样式来覆盖 textColor。
这是我当前的设置:
我在我的项目中使用 AppCompat-Library:
compile 'com.android.support:appcompat-v7:22.2.0'
我定义了以下主题:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/primary</item>
<item name="colorPrimaryDark">@color/primary_dark</item>
<item name="colorAccent">@color/accent</item>
<item name="android:textColorPrimary">@color/black</item> <!-- All TextViews should have this color as default text color -->
</style>
在 AndroidManifest.xml 中设置:
<application
android:name=".core.BaseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
此外,我定义了一些样式来更改某些 TextView 的 textAppearance:
<style name="App.Heading" parent="android:TextAppearance.Widget.TextView">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/red</item>
</style>
<style name="App.Heading.Highlighted" parent="android:TextAppearance.Widget.TextView">
<item name="android:textSize">16sp</item>
<item name="android:textColor">@color/blue</item>
</style>
现在我有了一个带有一些 TextView 的 xml 布局。其中一些应该具有默认的 textAppearance(实际上是黑色文本颜色,在我的主题中定义为 android:textColorPrimary)。有些人应该应用我定义的样式中的自定义文本外观:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="CUSTOM: App.Heading"
android:textAppearance="@style/App.Heading" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="CUSTOM: App.Heading.Highlighted"
android:textAppearance="@style/App.Heading.Highlighted" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Normal Textview with no style"/>
前两个 TextView 应用了我定义的 textAppearance,这很好。但是最后一个 TextView 有一个深灰色的文本颜色(Material Design 默认值?)而不是黑色的。如果我设置属性:
<item name="android:textColor">@color/black</item>
在我的 AppTheme 中,所有 TextView 的文本颜色都是黑色。我的样式(例如 App.Heading)中定义的文本颜色不再被识别。
所以我的问题是:如何为我的 TextView 设置默认的 textColor,它可以通过设置 textAppearance 来覆盖?
最佳答案
找到方法并不容易,因为有 3 个可样式化的 android:textColor
属性,textColorPrimary textColorSecondary 和 textColorTertiary,还有很多基本样式的属性 android:textAppearance
作为小型媒体等(现在已弃用?)。
无论如何,关于没有样式的 TextView,它似乎默认指的是 android:textAppearanceSmall
可样式属性,其值为样式 TextAppearance.AppCompat.Small
覆盖通过 Base.TextAppearance.AppCompat.Small
具有 ?android:attr/textColorTertiary
值的 android:textColor
属性。
然后像下面这样覆盖它就可以了:
<item name="android:textColorTertiary">@color/black</item>
关于android - AppCompat v22 : Set default textcolor for TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31425427/