Is there a way to set drawable's Alpha using XML?

后端 未结 9 722
无人及你
无人及你 2020-12-24 05:21

Easy like itself . I wanna make an alpha button , which would have a selected drawable this way:




        
相关标签:
9条回答
  • 2020-12-24 05:44

    NOTE: This answer is obsolete. See the answer by kikettas for how to solve OP's problem using the compatibility library. (I would delete this answer, but SO won't let me because OP marked it as the solution.)

    Original answer:

    I don't think there's a way to do that. There is an attribute android:alpha, but it applies to views, not drawables. It's only been available since API level 11.

    0 讨论(0)
  • 2020-12-24 05:45

    I achieved the same using a drawable

    <?xml version="1.0" encoding="UTF-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <solid android:color="#5000ddff" />
    </shape>
    

    Over here used the alpha 50, which sets the opacity level. Hope that helps

    0 讨论(0)
  • 2020-12-24 05:45

    I have been looking for the same thing. Even though this is posted over four years ago, this is the top post when googling the issue, so I'll reply here.

    This is my solution

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="false">
            <bitmap android:alpha="@integer/not_pressed_alpha" android:src="@drawable/item"/>
        </item>
        <item android:state_pressed="true" android:drawable="@drawable/item" />
    </selector>
    
    0 讨论(0)
提交回复
热议问题