简体   繁体   中英

Theme, Styles and Alias nesting not working

I'm trying to test a way to make themes, but the approach I used isn't giving me the expected results. Here my setup:

drawable/dummy.xml

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/icon" />

values/mythemes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="MyOrange" parent="android:style/Theme">
        <item name="@drawable/dummy">@style/imgOrange</item>
    </style>
    <style name="MyBlue" parent="android:style/Theme">
        <item name="@drawable/dummy">@style/imgBlue</item>
    </style>

    <style name="imgOrange">
        <item name="android:src">@drawable/orange</item>
    </style>
    <style name="imgBlue">
        <item name="android:src">@drawable/blue</item>
    </style>
</resources>

layuot/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:orientation="vertical">
    <ImageView android:id="@+id/imageView1"
        android:src="@drawable/dummy"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">
        </ImageView>
</LinearLayout>

I don't get any error, but it doesn't change theme either.

I tryed to use also a style as a "dummy" but it does the same thing.

Any idea/explanation?

I do not pretend to the correctness of the decision, but it works. I solved the problem by this trick:

My attrs:

<attr name="arrow_up_active" format="reference" />
<attr name="arrow_down_active" format="reference" />

My themes:

<style name="AppThemeLight" parent="Theme.AppCompat.Light.NoActionBar">
    ....
    <item name="arrow_down_block">@drawable/up_active</item>
    <item name="arrow_down_active">@drawable/down_active</item>
</style>
<style name="AppThemeDark" parent="Theme.AppCompat.NoActionBar">
    ....
    <item name="arrow_down_block">@drawable/up_active</item>
    <item name="arrow_down_active">@drawable/down_active</item>
</style>

My var's:

private TypedArray arrowUpActive, arrowDownActive;

Declaration:

arrowUpActive = getActivity().getTheme().obtainStyledAttributes(new int[] { R.attr.arrow_up_active });
arrowDownActive = getActivity().getTheme().obtainStyledAttributes(new int[] { R.attr.arrow_down_active });

And usage:

imageButton.setImageDrawable(getResources().getDrawable(arrowUpActive.getResourceId(0,1)));
imageButton.setImageDrawable(getResources().getDrawable(arrowDownActive.getResourceId(0,1)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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