覆盖引用的样式属性 [英] Overriding referenced style attributes
问题描述
到<一看完后href="http://developer.android.com/guide/topics/resources/accessing-resources.html#ReferencesToThemeAttributes">References为主题属性我想引用我已经设置了我的自定义主题的属性值。
After reading through References To Theme Attributes I am trying to reference the value of an attribute in the my custom theme that I have set.
我申请一个用户定义的样式为 CheckedTextView
I am applying a user-defined style to a CheckedTextView
<CheckedTextView
android:id="@+id/contactInfo"
style="@style/ListViewCheckedTextViewRowStyle" >
</CheckedTextView>
用户定义风格定义为:
The user-defined style is defined as :
<style name="ListViewCheckedTextViewRowStyle" parent="@style/ListViewRowStyle">
<item name="android:checkMark">?android:listChoiceIndicatorMultiple</item>
</style>
我的主题我创建的定义是:
My theme I created is defined as :
<style name="Theme.Yellowgreen" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:listChoiceIndicatorMultiple">@drawable/btn_check_holo_light</item>
</style>
但是,被显示的对号造型是设备的默认主题的绘制,而不是我的用户定义的绘制。
However, the checkMark styling that gets displayed is the device's default theme's drawable and not my user defined drawable.
我可以有我的显示绘制的唯一方法是:
The only way I can have my drawable displayed is with :
<style name="ListViewCheckedTextViewRowStyle" parent="@style/ListViewRowStyle">
<item name="android:checkMark">@drawable/btn_check_holo_light</item>
</style>
但是,这违背了重写此属性,尤其是因为我想重写这个属性在多个主题的整个目的。
But that defeats the whole purpose of overriding this attribute, especially since I would like to override this attribute in multiple themes.
我设置的主题在我的活动的
是这样的:的onCreate()
方法
I am setting the theme in the onCreate()
method of my Activity
like this:
public void onCreate(Bundle savedInstanceState) {
this.setTheme(R.style.Theme_Yellowgreen);
super.onCreate(savedInstanceState);
// ...
}
我也试过来设置主题,在AndroidManifest.xml文件,如:
I also tried to set the theme in the AndroidManifest.xml file like :
<application android:theme="@style/Theme.Yellowgreen" >
但没有奏效。可能是什么问题呢?
But that didn't work. What could be going wrong?
我刚刚创建了一个小样本项目,它看起来像codeI上面张贴的工作。所以,我必须有一个要重写这个属性的一些其他风格或许它与我的布局的XML文件。
I just created a small sample project and it looks like the code i posted above is working. So i must have some other styles which are overriding this property or perhaps it has to do with my layout xml files.
在我大的项目,我有两个片段
的活动中的
。无论片段
的列表视图
背靠适配器
。在片段A
的适配器
如下: getView()
方法
In my large project, I have two Fragments
within an Activity
. Both Fragments
have Listviews
backed by Adapters
. In Fragment A
the getView()
method of the Adapter
is as follows:
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.contact_entry, null);
}
//...
return convertView;
}
在片段B
的适配器
如下: getView()
方法
In Fragment B
the getView()
method of the Adapter
is as follows:
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, parent, false);
}
//...
return convertView;
}
的布局定义如下:
The layouts are defined as follows:
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/list_item_header" />
<include layout="@layout/contact_entry" />
<View android:id="@+id/list_divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@android:drawable/divider_horizontal_dark" />
</LinearLayout>
list_item_header.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/header_text"
android:layout_width="match_parent"
android:layout_height="25dip"
android:background="@color/dark_blue"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:textColor="@color/white"
android:textSize="14sp"
android:textStyle="bold" />
contact_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contactEntry"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal" >
<QuickContactBadge
android:id="@+id/contactPic"
style="@style/ContactPicStyle" />
<CheckedTextView
android:id="@+id/contactInfo"
style="@style/ListViewCheckedTextViewRowStyle" >
</CheckedTextView>
</LinearLayout>
由于某些原因,在片段B
的主题复选标记属性不正确地呈现,而在片段A
中,勾选使用当前黄绿主题和正确的风格。为什么会这样发生?
For some reason, in Fragment B
the themed checkMark attribute is not rendering correctly whereas in Fragment A
, the checkMark uses the current YellowGreen Theme and is styled correctly. Why would this be happening?
推荐答案
我认为你需要这条线在你的主题
I think you need that line in your theme
<item name="android:checkedTextViewStyle">@style/ListViewCheckedTextViewRowStyle</item>
所以它是这样的:
so it would look like this:
<style name="Theme.Yellowgreen" parent="android:Theme.Holo.Light.DarkActionBar">
<item name="android:listChoiceIndicatorMultiple">@drawable/btn_check_holo_light</item>
<item name="android:checkedTextViewStyle">@style/ListViewCheckedTextViewRowStyle</item>
</style>
更新
问题的更新和一些额外的信息形式的意见后,我们发现这个问题是在上下文
。这是同样的错误,我做了我的问题:<一href="http://stackoverflow.com/questions/9005015/android-color-selector-doesnt-work-with-custom-attributes">Android颜色选择不与自定义的工作属性
After question update and some additional information form comments we found that the problem was in Context
. It's the same mistake I did in that my question: Android color selector doesn't work with custom attributes
要一个片段通过了活动
和其他应用程序
。我不是专家,但即使是这两个类都扩展上下文
仅活动
扩展 ContextThemeWrapper
,里面有关于样式的信息。它可能是有益的阅读文章对未来的理解上下文:<一href="http://www.doubleencore.com/2013/06/context/">http://www.doubleencore.com/2013/06/context/
To one fragment was passed Activity
and to the other Application
. I'm not an expert but even both of those classes are extending Context
only the Activity
extends ContextThemeWrapper
, which has information about styles. It might be helpful to read that article for future understanding Context: http://www.doubleencore.com/2013/06/context/
这篇关于覆盖引用的样式属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!