我使用xml文件爲我的按鈕着色。在xml中有一個代碼來定義點擊按鈕時我的按鈕的顏色。如何設置按鈕的狀態_按下
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<gradient
android:startColor="@color/mainRaddoppiaButtonBackgroundEnd"
android:endColor="@color/mainRaddoppiaButtonBackgroundStart"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/mainRaddoppiaButtonBackgroundStart" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:startColor="@color/mainRaddoppiaButtonBackgroundEnd"
android:endColor="@color/mainRaddoppiaButtonBackgroundStart"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/mainRaddoppiaButtonBackgroundStart" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="@color/mainRaddoppiaButtonBackgroundStart"
android:endColor="@color/mainRaddoppiaButtonBackgroundEnd"
android:angle="270" />
<stroke
android:width="0dp"
android:color="@color/mainRaddoppiaButtonBackgroundStart" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
是否可能從java保持我的按鈕被按下,並使用在我的xml文件中設置的「state_pressed」值進行着色?
成才,如:
public void onClick(View button) {
button.seLayout(R.xml.xmlFileName.state_pressed)
}
當然這個代碼有沒有意義,我只是做它讓你明白我想要做
更換和BTN_PRESSED與btn_normal要各自繪製。 –
我需要用java做 – MDP
button.setBackgroundResource(R.drawable.xmlimage); –