colorAccent不能被子主题覆盖

时间:2016-07-29 20:50:17

标签: android android-styles

我有整个应用程序的基本样式,以及文本视图的子样式。但在儿童风格中," accentColor"项目没有生效。文本视图仍使用父样式的accentColor。

这是我的style.xml:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">#00ff00</item> <!-- Green color -->
</style>

<style name="MyCustomTheme" parent="AppTheme">
    <item name="colorAccent">#ff0000</item> <!-- Red color -->
</style>

</resources>

这是我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="io.github.seemuch.highlightexperiment.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textIsSelectable="true"
    android:textColorHighlight=""
    style="@style/TextViewTheme"/>

</RelativeLayout>

以下是该应用的屏幕截图: screenshot

如您所见,文本视图的高亮颜色为绿色,即AppTheme的accentColor,而不是TextViewTheme&#39; s。

我只是想改变文字高亮的颜色。我知道可以通过添加&#34; textColorHighlight&#34;来改变高亮部分本身。属性为文本视图,但不会改变&#34;锚点#34;的颜色。我发现的唯一方法是改变强调色,但我也不想改变整个应用程序的强调色。

有人知道如何让儿童风格的accentColor工作吗?或者有更好的方法来改变锚点&#39;颜色?

1 个答案:

答案 0 :(得分:1)

colorAccent是主题属性,而不是Themes and Styles I/O 2016 talk的样式属性。

因此,您需要将其应用于android:theme,而不是style

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Hello World!"
  android:textIsSelectable="true"
  android:textColorHighlight=""
  android:theme="@style/MyCustomTheme"/>

您的MyCustomTheme通常应该有ThemeOverlay.AppCompat的父级,因为它会覆盖默认主题(仅更改其定义的内容),而不是重置父主题中的所有内容。