android - 在颜色选择器中使用 appcompat 的 ?colorAccent 似乎不起作用

标签 android android-appcompat

我正在尝试在文本颜色选择器元素内使用“?colorAccent”,但只要相关状态被激活,文本就会显示红色而不是我的实际 colorAccent 值。我已将问题隔离到最少的文件,并在下面发布了相关 fragment 。我还在此处上传了完整的项目:https://github.com/danh32/ColorAccentSelector如果有任何进一步的帮助。

1) 我将一个 ListView 设置为单选模式,以便可以检查它的行。

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView listView = (ListView) findViewById(R.id.listview);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        listView.setAdapter(new ItemAdapter());
    }


}

2) 每行只是一个 CheckedTextView,因此我可以根据选中状态操作其 textColor。行.xml:

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="16sp"
    android:padding="8sp"
    android:textColor="@color/row_text_color" />

3) @color/row_text_color 具有以下内容,未选中时应显示纯黑色,选中时应显示我的 colorAccent 值:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="?colorAccent" />
    <item android:color="@android:color/black" />
</selector>

4) 我的应用程序的主题是这样的,应该将 ?colorAccent 值设置为##ff4081( Material 粉红色 A200):

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->

        <item name="colorPrimary">#3f51b5</item>
        <item name="colorPrimaryDark">#303f9f</item>
        <item name="colorAccent">#ff4081</item>

    </style>

</resources>

但当我运行时,我仍然看到以下内容:

screenshot

如果我直接将 CheckedTextView 更改为具有 textColor="?colorAccent"而不是使用选择器,则颜色为正确的粉红色值。有没有办法让它在选择器中起作用?

最佳答案

感谢https://stackoverflow.com/a/23210511/462252对于答案,显然你不能在颜色选择器中使用 attr 引用。我最终采纳了他的建议并创建了一个名为 textColorSelector 的特定于我的应用程序的新属性,以便我可以在主题中更换我的选择器。

如果有人觉得有用,我很乐意分享更多细节。

编辑:我使用的代码:

属性.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="checkedTextSelector" format="reference"/>
</resources>

themes.xml(注意每个子主题指定不同的“checkedTextSelector”):

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- specify enter and exit transitions -->
        <item name="android:windowContentTransitions" tools:targetApi="21">true</item>
        <item name="android:windowEnterTransition" tools:targetApi="21">@android:transition/explode</item>
        <item name="android:windowExitTransition" tools:targetApi="21">@android:transition/explode</item>
    </style>

    <style name="AppTheme.Blue">
        <item name="colorPrimary">@color/material_blue_500</item>
        <item name="colorPrimaryDark">@color/material_blue_700</item>
        <item name="colorAccent">@color/material_purple_a400</item>
        <item name="checkedTextSelector">@color/nav_row_text_color_blue</item>
    </style>

    <style name="AppTheme.Red">
        <item name="colorPrimary">@color/material_red_500</item>
        <item name="colorPrimaryDark">@color/material_red_700</item>
        <item name="colorAccent">@color/material_light_blue_a400</item>
        <item name="checkedTextSelector">@color/nav_row_text_color_red</item>
    </style>

    <style name="AppTheme.Pink">
        <item name="colorPrimary">@color/material_pink_500</item>
        <item name="colorPrimaryDark">@color/material_pink_700</item>
        <item name="colorAccent">@color/material_light_blue_a400</item>
        <item name="checkedTextSelector">@color/nav_row_text_color_pink</item>
    </style>

    <style name="AppTheme.Purple">
        <item name="colorPrimary">@color/material_purple_500</item>
        <item name="colorPrimaryDark">@color/material_purple_700</item>
        <item name="colorAccent">@color/material_blue_a400</item>
        <item name="checkedTextSelector">@color/nav_row_text_color_purple</item>
    </style>

    <style name="AppTheme.Green">
        <item name="colorPrimary">@color/material_green_500</item>
        <item name="colorPrimaryDark">@color/material_green_700</item>
        <item name="colorAccent">@color/material_purple_a400</item>
        <item name="checkedTextSelector">@color/nav_row_text_color_green</item>
    </style>
</resources>

nav_row_text_color_blue.xml(其他文件只是指定不同的颜色):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/material_blue_500" />
    <item android:state_checked="true" android:color="@color/material_purple_a400"/>
    <item android:color="@color/text_black_primary"/>
</selector>

关于android - 在颜色选择器中使用 appcompat 的 ?colorAccent 似乎不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26942734/

相关文章:

带有 VectorDrawables srcCompat 的 Android Selector Drawable

Android Studio 3.0 合并调试资源失败 - 错误 : file not found

java - 带有底部按钮的滑动窗口

java - 检查两个 View 之间 Activity 的碰撞检测

android - Gradle 构建错误 ."minsdkversion 14 cannot be different than version l declared in library"?

android - 添加媒体路由器转换按钮时出错

android - AppCompat fragment 生命周期已更改

java - 为什么小部件的构造需要上下文?

android - 如何在 ViewGroup 级别处理所有 TouchEvent(即冒泡所有事件直至包含 View 组)

android - Volley 没有第二次调用 getParams()