简体   繁体   English

Android TextView使用Theme更改textColor

[英]Android TextView change textColor with Theme

I am trying to put all my Styles into a Theme for my app. 我想把我的所有样式放到我的应用程序的主题中。 However, I'm having trouble changing the textColor on a TextView. 但是,我在TextView上更改textColor时遇到了问题。 This is from my attrs.xml file. 这是来自我的attrs.xml文件。

<?xml version="1.0" encoding="utf-8"?>

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

</declare-styleable>

This is from my styles.xml file 这是来自我的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>

And this is from my layout.xml file 这是来自我的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" />

I also have the manifest set up correctly with the following. 我还使用以下内容正确设置了清单。

    <application
    android:name="com.example.blah"
    android:icon="@drawable/blah"
    android:label="@string/app_name"
    android:theme="@style/flat_theme"

The color never gets changed to the "@color/detail_group_black". 颜色永远不会变为“@ color / detail_group_black”。 Am I missing something simple? 我错过了一些简单的事吗? Or is the problem that Android is already creating a textColor attribute for the TextView and that's why the style isn't overriding it? 或者问题是Android已经为TextView创建了一个textColor属性,这就是为什么样式没有覆盖它? My attribute-theme strategy seems to be working well for the rest of the app but not this. 我的属性主题策略似乎适用于应用程序的其余部分但不是这个。 Any ideas? 有任何想法吗? Thanks in advance. 提前致谢。

Also, if it can be helped I'd rather not create a custom TextView class just for this change: its a fairly large project and I'm not sure what kind of problems I might run into if I extend TextView for everything. 此外,如果它可以帮助我宁愿不为这个改变创建一个自定义TextView类:它是一个相当大的项目,我不知道如果我扩展TextView的一切可能会遇到什么样的问题。

Edit Also, if I change the TextView to have style="@style/flat_detail_name_view", it works fine. 编辑此外,如果我将TextView更改为style =“@ style / flat_detail_name_view”,它可以正常工作。 The reason I don't want to do that is so that I can change the appearance of these textviews just by changing the theme. 我不想这样做的原因是我可以通过更改主题来更改这些文本视图的外观。 Using style="?something_else" works on all my other views, so I really don't understand this and I'm probably missing something simple? 使用style =“?something_else”适用于我所有的其他视图,所以我真的不明白这一点,我可能会遗漏一些简单的东西?

I found my problem, in case anyone runs into a similar situation! 我发现了我的问题,万一有人遇到类似的情况! Basically the reason this wasn't working is because I was inflating the view with the following code. 基本上这不起作用的原因是因为我用以下代码给视图充气。

    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;
}

And in my Adapter's getGroupView I was returning it. 在我的Adapter的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;
}

Apparently my problem was that I needed to replace LayoutInflater 显然我的问题是我需要替换LayoutInflater

inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

with .. 与......

LayoutInflater inf = mActivity.getLayoutInflater();

As suggested by CommonsWare at the bottom of this post: Theme/Style is not applied when inflater used with ApplicationContext . 正如CommonsWare在本文底部所建议的: 当inflater与ApplicationContext一起使用时,不应用Theme / Style

Thanks for your help. 谢谢你的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM