主题、样式和别名嵌套不起作用

发布于 2024-11-15 13:37:15 字数 1542 浏览 1 评论 0原文

我正在尝试测试一种制作主题的方法,但我使用的方法没有给我预期的结果。这是我的设置:

drawable/dummy.xml

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

value/mythemes.xmllayuot

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

/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'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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美男兮 2024-11-22 13:37:15

我不会假装这个决定是正确的,但它确实有效。
我通过这个技巧解决了这个问题:

我的属性:

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

我的主题:

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

我的变量:

private TypedArray arrowUpActive, arrowDownActive;

声明:

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

和用法:

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

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)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文