我在整个应用程序中使用了一个主题,在主题中文本颜色简单地声明如下:
android:textColor="@android:color/black"
对于位于活动内部的TextView,颜色应用得很好.但是,当我在列表项内部创建一个TextView(由适配器充气)时,文本颜色为白色.
有谁知道需要在主题中声明要影响列表项的样式?
列表项的完整XML:
<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>
Run Code Online (Sandbox Code Playgroud)
列表的XML:
<ListView
android:id="@+id/altl_list"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Run Code Online (Sandbox Code Playgroud)
和适配器:
@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;
}
Run Code Online (Sandbox Code Playgroud)
发生这种情况是因为你ListView用Context你的Application(LayoutInflater.from(getApplicationContext());或其他衍生物)膨胀你的行布局,而不是使用你Activity的Context.
您将获得的上下文之间的差异,getApplicationContext()并为其中任何一个Activity.this的实现奠定Context了基础.
两者Context,Application's Context和Activity's被包含在内部ContextWrapper并代理他们对内部"Base"的调用Context.
这里的不同之处在于Application该类是一个单例,并且只有一个全局实现用于整个Application,而每个Activity - 自从继承ContextWrapper- 都有自己独特的实现Activity.
现在,这就是为什么getApplication()不 - 也不能 - 遵守您在清单中定义的主题规则,而是Context由您的Activity/可以提供.
我强烈建议你阅读Dave Smith 关于s 的文章Context.
| 归档时间: |
|
| 查看次数: |
247 次 |
| 最近记录: |