覆盖引用的样式属性

too*_*o42 9 android themes styles overriding checkedtextview

阅读参考主题属性后,我试图在我设置的自定义主题中引用属性的值.

我将用户定义的样式应用于 CheckedTextView

<CheckedTextView
    android:id="@+id/contactInfo"
    style="@style/ListViewCheckedTextViewRowStyle" >
</CheckedTextView>
Run Code Online (Sandbox Code Playgroud)

用户定义的样式定义为:

<style name="ListViewCheckedTextViewRowStyle" parent="@style/ListViewRowStyle">
    <item name="android:checkMark">?android:listChoiceIndicatorMultiple</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我创建的主题定义为:

<style name="Theme.Yellowgreen" parent="android:Theme.Holo.Light.DarkActionBar">
    <item name="android:listChoiceIndicatorMultiple">@drawable/btn_check_holo_light</item>
</style>
Run Code Online (Sandbox Code Playgroud)

但是,显示的checkMark样式是设备的默认主题的drawable,而不是我的用户定义的drawable.

我可以显示可绘制的唯一方法是:

<style name="ListViewCheckedTextViewRowStyle" parent="@style/ListViewRowStyle">
    <item name="android:checkMark">@drawable/btn_check_holo_light</item>
</style>
Run Code Online (Sandbox Code Playgroud)

但这违背了覆盖此属性的整个目的,特别是因为我想在多个主题中覆盖此属性.

我在onCreate()我的方法中设置主题Activity:

public void onCreate(Bundle savedInstanceState) {
    this.setTheme(R.style.Theme_Yellowgreen);
    super.onCreate(savedInstanceState);
    // ...
}
Run Code Online (Sandbox Code Playgroud)

我还尝试在AndroidManifest.xml文件中设置主题,如:

<application android:theme="@style/Theme.Yellowgreen" >
Run Code Online (Sandbox Code Playgroud)

但那没用.怎么可能出错?

更新

我刚刚创建了一个小型示例项目,看起来我上面发布的代码正在运行.所以我必须有一些其他样式覆盖这个属性,或者它可能与我的布局xml文件有关.

在我的大项目中,我有两个FragmentsActivity.两者Fragments都有Listviews支持Adapters.在Fragment A所述getView()的方法Adapter如下:

public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.contact_entry, null);
    }

    //...

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

Fragment B所述getView()的方法Adapter如下:

public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, parent, false);
    }

    //...

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

布局定义如下:

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>
Run Code Online (Sandbox Code Playgroud)

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" />
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

出于某种原因,在Fragment B主题中checkMark属性未正确呈现,而在中Fragment A,checkMark使用当前的YellowGreen主题并且样式正确.为什么会这样?

Mik*_*oos 11

我认为你的主题需要这一行

<item name="android:checkedTextViewStyle">@style/ListViewCheckedTextViewRowStyle</item>
Run Code Online (Sandbox Code Playgroud)

所以它看起来像这样:

<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>
Run Code Online (Sandbox Code Playgroud)

UPDATE

在问题更新和一些其他信息形式评论后,我们发现问题出在了Context.这与我的问题中的错误相同:Android颜色选择器不适用于自定义属性

为了一个片段,通过Activity和其他Application.我不是专家,但即使这两个类Context都只Activity扩展了ContextThemeWrapper包含样式信息的扩展.阅读该文章以便将来理解可能会有所帮助上下文:http://www.doubleencore.com/2013/06/context/

  • mInflater的上下文是什么?如果使用context.getApplicationContext()将上下文传递给Adapter,它将无法正确设置样式.您需要传递Activities上下文. (4认同)