Setting alpha on a BitmapDrawable in XML
I was wondering if its possible to set transparency in XML for a Drawable. In this case its setting the disabled state of a button. Instead of using a separate image i should like to create a gr开发者_运维百科ayed-out version by changing the alpha. I know I could do that in code. But can it also be predefined in XML?
Since: API Level 11 it is possible for View. but if we are talking about ImageView there is a tint attribute ans it's since API Level 1.
http://developer.android.com/reference/android/widget/ImageView.html#attr_android:tint
http://developer.android.com/reference/android/view/View.html#attr_android:alpha
I have been looking for the same thing. Even though this is posted over four years ago, this is one of 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_enabled="false">
<bitmap android:alpha="@integer/disabled_alpha" android:src="@drawable/item"/>
<item/>
<item android:state_enabled="true" android:drawable="@drawable/item" />
</selector>
精彩评论