tag requires a 'drawable' attribute

前端 未结 3 1558
渐次进展
渐次进展 2021-02-09 01:30

I am trying to very simply style a Button. I just want it blue with with text when not pressed, and white with blue text when clicked.

I tried to do this w

相关标签:
3条回答
  • 2021-02-09 01:50

    android:background seems to accept drawable selectors but not color selectors... so just leave a color as your android:background, and use a normal color selector with android:backgroundTint, which expects colors anyway:

    at styles.xml:

    <style name="MyButton">
        <item name="android:background">@color/some_fixed_color</item>
        <item name="android:backgroundTint">@color/background_color_selector</item>
        <item name="android:textColor">@drawable/btn_textcolor</item>
    </style>
    

    (res/color/background_color_selector.xml is a normal color selector.)

    0 讨论(0)
  • 2021-02-09 02:04

    Try this way,hope this will help you to solve your problem.

    btn_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@color/white" android:state_pressed="true"></item>
        <item android:drawable="@drawable/white" android:state_focused="true"></item>
        <item android:drawable="@drawable/SapphireBlue" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
        <item android:drawable="@drawable/white" android:state_enabled="false"></item>
    </selector>
    

    btn_textcolor.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/SapphireBlue" android:state_pressed="true"></item>
    <item android:color="@drawable/SapphireBlue" android:state_focused="true"></item>
    <item android:color="@drawable/white" android:state_enabled="true" android:state_focused="false" android:state_pressed="false"></item>
    <item android:color="@drawable/SapphireBlue" android:state_enabled="false"></item>
    </selector>
    
    0 讨论(0)
  • 2021-02-09 02:09
    • Background attribute expects a drawable and the drawable selector must be in one of the /drawable folders.
    • TextColor attribute expects a color and the color selector must be in one of the /color folders.
    /res/color/my_textcolor_selctor.xml
    /res/drawable/my_background_selector.xml
    

    Also, the drawable selector must use 'android:drawable' for selector items (a color resource maybe used for the attribute value since a color is a drawable), whereas the color selector must use 'android:color' for the selector items.

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_enabled="false"
            android:drawable="@color/disabledbackgroundColor" />
        <!-- default -->
        <item android:drawable="@color/myBackgroundColor" />
    </selector>

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_enabled="false"
            android:color="@color/disabledColor" />
        <!-- default -->
        <item android:color="@color/defaultColor" />
    </selector>

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