【发布时间】:2015-07-15 08:38:31
【问题描述】:
我想在我的 AppTheme 中设置一个默认的文本颜色,它应该是黑色(不是默认的 Material Design 深灰色)。应该通过在 UI 元素(例如 TextView)上通过 android:textAppearance-attribute 设置自定义样式来覆盖 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">
另外我定义了一些样式来改变一些TextViews的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 有一个深灰色的文本颜色(材料设计默认?)而不是黑色的。如果我设置属性:
<item name="android:textColor">@color/black</item>
在我的 AppTheme 中,所有的 TextView 都有黑色的文本颜色。我的样式中定义的文本颜色(例如 App.Heading)不再被识别。
所以我的问题是:如何为我的 TextView 设置默认的 textColor,可以通过设置 textAppearance 来覆盖它?
【问题讨论】:
-
克里斯托弗;你有解决办法吗?
-
@VahidGhadiri 抱歉,没有直接的解决方案。我使用了下面提到的解决方法。
-
如果您将主题
AppTheme设置为这些TextViewsViewGroup,它是否有效。也许它只是忘记了是什么。让我知道 -
使用 android:textColorTertiary 选项。我遇到过类似的问题,textColorTertiary 解决了问题
标签: android textview android-appcompat