如何从drawable引用到样式

Geo*_*zov 8 android themes styles image reference

带有标签的我的应用有两个主题.在每个主题中,选项卡具有处​​于选定和未选定状态的不同图像.我如何按主题正确引用图像?

例如.我有themes.xml

<?xml version="1.0" encoding="utf-8"?>

<style name="LightTheme" parent="@android:style/Theme.Light">
    <item name="tabShows">@drawable/ic_tab_shows_unselected_light</item>
    <item name="tabShowsSelected">@drawable/ic_tab_shows_selected_light</item>
    <item name="tabNews">@drawable/ic_tab_news_selected_light</item>
    <item name="tabNewsSelected">@drawable/ic_tab_news_unselected_light</item>
</style>

<style name="DarkTheme" parent="@android:style/Theme.Black">
    <item name="tabShows">@drawable/ic_tab_shows_unselected_dark</item>
    <item name="tabShowsSelected">@drawable/ic_tab_shows_selected_dark</item>
    <item name="tabNews">@drawable/ic_tab_news_selected_dark</item>
    <item name="tabNewsSelected">@drawable/ic_tab_news_unselected_dark</item>
   </style>
Run Code Online (Sandbox Code Playgroud)

我还有一个tab_shows.xml和tab_news.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item  android:state_selected="true" android:drawable="@drawable/ic_tab_shows_selected_light"/>
<item  android:state_selected="false" android:drawable="@drawable/ic_tab_shows_unselected_light" />
Run Code Online (Sandbox Code Playgroud)

如何根据当前主题在选择器中引用所需的图像?这不适合我

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item  android:state_selected="true" android:drawable="?tabShowsSelected"/>
<item  android:state_selected="false" android:drawable="?tabShows" />
Run Code Online (Sandbox Code Playgroud)

在布局文件中这是有效的,我的意思是通过?styleName引用样式

Luk*_*kap 5

建立你的风格A和风格B

在你的情况下,你把android:drawable="@drawable/ic_tab_shows_selected_light"而不是背景(我只是从我的代码复制了snipets)#000

    <style name="styleB">
        <item name="android:background">#000</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

你的主题A.

<style name="Theme.A">
        <item name="pageBackground">@style/styleA</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

主题B.

<style name="Theme.Blue">
        <item name="pageBackground">@style/styleB</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

在你的attr.xml中

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="pageBackground" format="reference" />
</resources>
Run Code Online (Sandbox Code Playgroud)

最后在你的小部件中你做 style="?pageBackground"


Luk*_*kap 3

您可以在这里找到答案\n http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

\n\n

编辑
\n(Lukap 在评论中提供的附加信息)

\n\n
    \n
  1. 定义一个或多个主题themes.xml并在那里设置样式的定义。
  2. \n
  3. 在 中定义自定义属性,也称为自定义样式attrs.xml
  4. \n
  5. 描述您的自定义样式的值是什么styles.xml
  6. \n
\n\n

但您需要阅读更多有关attrs.xml

\n\n
<item name="android:background">? android:attr/activatedBackgroundIndicator</item> \n</style> \n
Run Code Online (Sandbox Code Playgroud)\n\n

相反,我们引用activatedBackgroundIndicator继承主题中的一些其他属性 \xe2\x80\x93 \xe2\x80\x93 的值。无论主题定义是什么,activatedBackgroundIndicator我们的背景就应该是什么。

\n