Android的主题不影响列表项里面TextViews [英] Android theme not affecting TextViews inside list item

查看:134
本文介绍了Android的主题不影响列表项里面TextViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在整个应用程序使用的主题,在主题的文本颜色只是声明如下:

机器人:文字颜色=@机器人:彩色/黑白

对于位于一个活动里面一个TextView的颜色应用就好了。然而,当我创建一个列表项内一个TextView(通过适配器充气)的文本颜色为白色,而不是

有谁知道需要在主题被宣布为受影响的列表项是什么风格?

完整的XML列表项目:


 <复选框
    机器人:ID =@ + ID / lt_checkbox
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_margin =8DP
    机器人:layout_centerVertical =真
    机器人:layout_alignParentRight =真/><的TextView
    机器人:ID =@ + ID / lt_length
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_centerVertical =真
    机器人:layout_toLeftOf =@ ID / lt_checkbox
    机器人:TEXTSIZE =24dp/><的LinearLayout
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =match_parent
    机器人:layout_margin =8DP
    机器人:layout_centerVertical =真
    机器人:layout_toLeftOf =@ ID / lt_length
    机器人:layout_alignParentLeft =真
    机器人:方向=垂直>    <的TextView
        机器人:ID =@ + ID / lt_title
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_marginBottom =12dp
        机器人:TEXTSIZE =18dp/>    <的TextView
        机器人:ID =@ + ID / lt_subtitle
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:TEXTSIZE =14dp/>< / LinearLayout中>

XML的名单:

 < ListView控件
    机器人:ID =@ + ID / altl_list
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent/>

和适配器:

  @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    LocalTrack轨道=的getItem(位置);    ItemHolder持有人;
    如果(convertView == NULL || convertView.getTag()== NULL){
        convertView = mInflater.inflate(R.layout.listem_track,父母,假);        持有人=新ItemHolder();
        holder.title =(TextView中)convertView.findViewById(R.id.lt_title);
        holder.length =(TextView中)convertView.findViewById(R.id.lt_length);
        holder.checkbox =(复选框)convertView.findViewById(R.id.lt_checkbox);        convertView.setTag(保持器);
    }
    其他
        支架=(ItemHolder)convertView.getTag();    holder.title.setText(track.getTitle());
    holder.length.setText(Helpbot.convertMillisToTrackLength(track.getLength()));
    holder.checkbox.setChecked(mSelectedTracks.contains(轨道));    返回convertView;
}


解决方案

这是因为你正在膨胀的排布置你的的ListView 上下文你的应用 LayoutInflater.from(getApplicationContext()); 或其它一些衍生物),而不是用活动的 上下文

您从 getApplicationContext() Activity.this 规定在执行获取上下文之间的差异上下文为其中一方。

两者上下文应用上下文活动的被包裹 ContextWrapper 和代理中的一个内部的基地<$ C $电话C>上下文。

这里的区别是,应用类是独立的,并只为您的整个应用程序对应一个全球范围内实施,而每个活动 - 因为从 ContextWrapper ,以及 - 有它的每个活动自己独特的实施

现在,这就是为什么 getApplication()不 - 并不能 - 服从你在你的清单中定义的主题的规则,但上下文你的活动提供确实/罐。

我强烈建议你阅读有关文章 >上下文秒。

I have a theme used throughout the app, in the theme the text color is simply declared like this:

android:textColor="@android:color/black"

For a TextView located inside of an activity the color is applied just fine. However when I create a TextView inside of a list item (inflated by an adapter) the text color is white instead.

Does anyone know what style needs to be declared in the theme for list items to be affected?

Full XML for the list item:

<CheckBox
    android:id="@+id/lt_checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"/>

<TextView
    android:id="@+id/lt_length"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@id/lt_checkbox"
    android:textSize="24dp"/>

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_margin="8dp"
    android:layout_centerVertical="true"
    android:layout_toLeftOf="@id/lt_length"
    android:layout_alignParentLeft="true"
    android:orientation="vertical">

    <TextView
        android:id="@+id/lt_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="12dp"
        android:textSize="18dp"/>

    <TextView
        android:id="@+id/lt_subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="14dp"/>

</LinearLayout>

XML for the list:

<ListView
    android:id="@+id/altl_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

And the adapter:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LocalTrack track = getItem(position);

    ItemHolder holder;
    if(convertView == null || convertView.getTag() == null) {
        convertView = mInflater.inflate(R.layout.listem_track, parent, false);

        holder = new ItemHolder();
        holder.title = (TextView) convertView.findViewById(R.id.lt_title);
        holder.length = (TextView) convertView.findViewById(R.id.lt_length);
        holder.checkbox = (CheckBox) convertView.findViewById(R.id.lt_checkbox);

        convertView.setTag(holder);
    }
    else
        holder = (ItemHolder) convertView.getTag();

    holder.title.setText(track.getTitle());
    holder.length.setText(Helpbot.convertMillisToTrackLength(track.getLength()));
    holder.checkbox.setChecked(mSelectedTracks.contains(track));

    return convertView;
}

解决方案

This happens because you are inflating the row layout of your ListView with the Context of your Application (LayoutInflater.from(getApplicationContext()); or some other derivative), instead of using your Activity's Context.

The difference between the Context you will get from getApplicationContext() and Activity.this lays in the implementation of Context for either of them.

Both Context, the Application's Context and the Activity's gets wrapped inside a ContextWrapper and proxy their calls to an internal "Base" Context.

The difference here is that the Application class is a singleton and only has one global implementation for your whole Application, while each Activity - since inheriting from ContextWrapper as well - has it's own unique implementation for each Activity.

Now that's why getApplication() doesn't - and can't - obey the rules of the themes you defined in your manifest, but the Context provided by your Activity does/can.

I highly recommend you to read Dave Smith's article about Contexts.

这篇关于Android的主题不影响列表项里面TextViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆