Android TextView使用Theme更改textColor

pic*_*tor 5 android themes styles textview

我想把我的所有样式放到我的应用程序的主题中.但是,我在TextView上更改textColor时遇到了问题.这是来自我的attrs.xml文件.

<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)

<declare-styleable name="flat">
    <attr name="detail_name_view" format="reference" />

</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

这是来自我的styles.xml文件

<style name="flat_theme" parent="android:Theme.NoTitleBar">
    <item name="detail_name_view">@style/flat_detail_name</item>
</style>
<style name="flat_detail_name" parent="@android:style/Widget.TextView">
    <item name="android:textAppearance">?android:attr/textAppearanceMedium</item>
    <item name="android:textColor">@color/detail_group_black</item>
    <item name="android:textSize">16sp</item> 
    <item name="android:singleLine">true</item>
    <item name="android:ellipsize">end</item>
</style>
Run Code Online (Sandbox Code Playgroud)

这是来自我的layout.xml文件

    <TextView
        android:id="@+id/detail_name"
        style="?detail_name_view"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_gravity="left|center_vertical"
        android:layout_marginLeft="5dp"
        android:layout_weight="1"
        android:gravity="left|center_vertical" />
Run Code Online (Sandbox Code Playgroud)

我还使用以下内容正确设置了清单.

    <application
    android:name="com.example.blah"
    android:icon="@drawable/blah"
    android:label="@string/app_name"
    android:theme="@style/flat_theme"
Run Code Online (Sandbox Code Playgroud)

颜色永远不会变为"@ color/detail_group_black".我错过了一些简单的事吗?或者问题是Android已经为TextView创建了一个textColor属性,这就是为什么样式没有覆盖它?我的属性主题策略似乎适用于应用程序的其余部分但不是这个.有任何想法吗?提前致谢.

此外,如果它可以帮助我宁愿不为这个改变创建一个自定义TextView类:它是一个相当大的项目,我不知道如果我扩展TextView的一切可能会遇到什么样的问题.

编辑 此外,如果我将TextView更改为style ="@ style/flat_detail_name_view",它可以正常工作.我不想这样做的原因是我可以通过更改主题来更改这些文本视图的外观.使用style ="?something_else"适用于我所有的其他视图,所以我真的不明白这一点,我可能会遗漏一些简单的东西?

pic*_*tor 2

我发现了我的问题,以防有人遇到类似的情况!基本上,这不起作用的原因是因为我使用以下代码来膨胀视图。

    private View buildGroupView(ExpandListGroup group) {
    LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    View view;
    switch(group.getType()){
        case ExpandListGroup.HEADER :
            view = inf.inflate(R.layout.three_col_header, null);
            break;
        default:
            view = inf.inflate(R.layout.detail_expandablelist_group_item, null);
            break;

    }

    return view;
}
Run Code Online (Sandbox Code Playgroud)

在我的适配器的 getGroupView 中我正在返回它。

    public View getGroupView(int groupPosition, boolean isLastChild, View convertView,
        ViewGroup parent) {
    View view = convertView;
    ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition);

    view = buildGroupView(group);

    setGroupViewData(group, view);

    return view;
}
Run Code Online (Sandbox Code Playgroud)

显然我的问题是我需要更换 LayoutInflater

inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
Run Code Online (Sandbox Code Playgroud)

和 ..

LayoutInflater inf = mActivity.getLayoutInflater();
Run Code Online (Sandbox Code Playgroud)

正如 CommonsWare 在这篇文章底部所建议的:当 Inflate 与 ApplicationContext 一起使用时,不会应用主题/样式

感谢您的帮助。