Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
556 views
in Technique[技术] by (71.8m points)

android - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...