How to reference from drawable to style

后端 未结 2 1511
庸人自扰
庸人自扰 2021-01-02 09:18

My app with tabs has two themes. In each theme tabs have different images in selected and unselected state. How I can properly reference to image by theme?

For examp

相关标签:
2条回答
  • 2021-01-02 09:36

    Build your style A and style B

    in your case you put android:drawable="@drawable/ic_tab_shows_selected_light" instead of background (I just copied snipets from my code) #000

        <style name="styleB">
            <item name="android:background">#000</item>
        </style>
    

    your theme A

    <style name="Theme.A">
            <item name="pageBackground">@style/styleA</item>
        </style>
    

    theme B

    <style name="Theme.Blue">
            <item name="pageBackground">@style/styleB</item>
        </style>
    

    in your attr.xml

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <attr name="pageBackground" format="reference" />
    </resources>
    

    finally in your widget you do style="?pageBackground"

    0 讨论(0)
  • 2021-01-02 09:38

    You can find your answer here http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

    Edit
    (Additional information by Lukap in comments)

    1. Define one or more themes in themes.xml and set the definitions of your styles there.
    2. Define custom attributes, a.k.a. custom styles, in attrs.xml.
    3. Describe what the values of your custom styles are in styles.xml.

    But you will need read more about the attrs.xml

    <item name="android:background">? android:attr/activatedBackgroundIndicator</item> 
    </style> 
    

    Instead, we are referring to the value of some other attribute – activatedBackgroundIndicator – from our inherited theme. Whatever the theme defines as being the activatedBackgroundIndicator is what our background should be.

    0 讨论(0)
提交回复
热议问题