问题
I'm writing an XML selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/image_state2" android:state_pressed="true"/>
<item android:drawable="@drawable/image_state1"/>
</selector>
I just want state1 to be transparent.So that the image to be seen as - is
state1
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#00000000"
android:startColor="#00000000" />
Now I want state2 to shade the pic a bit which I have accomplished already. My problem is that other than the shading gradient I want an icon to appear on the center. I don't know how to add this through xml
state2
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#80000000"
android:startColor="#80000000" />
回答1:
You can do it by using layer-list for ex:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle" >
<gradient
android:angle="270"
android:endColor="#80000000"
android:startColor="#80000000" />
</shape>
</item>
<item>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/background"
android:tileMode="repeat" android:gravity="center" />
</item>
</layer-list>
Save the above as @drawable/image_state2.
For more info: Resize bitmap inside a drawable layer-list
来源:https://stackoverflow.com/questions/24993309/how-can-i-add-an-image-in-my-custom-shape-xml