Android APP開發活用範例速查大辭典(Ch0108-利用XML來控制Button當按下時改變顯示顏色狀態)
GUI-XML
<?xml version=”1.0″ encoding=”utf-8″?> <LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android” android:layout_width=”fill_parent” android:layout_height=”fill_parent” android:gravity=”center_vertical” android:orientation=”vertical” >
<Button android:id=”@+id/buttonswitch” android:layout_width=”fill_parent” android:layout_height=”wrap_content” android:background=”@drawable/ch0108_selector_button” android:text=”狀態變化” />
</LinearLayout>
|
GUI-ch0108_selector_button
<?xml version=”1.0″ encoding=”utf-8″?> <selector xmlns:android=”http://schemas.android.com/apk/res/android”>
<!– 一般時 –> <item android:drawable=”@drawable/ch0108_bg_button_normal” android:state_pressed=”false”/>
<!– 按下時 –> <item android:drawable=”@drawable/ch0108_bg_button_pressed” android:state_pressed=”true”/>
</selector>
|
GUI-ch0108_bg_button_normal
<?xml version=”1.0″ encoding=”utf-8″?> <shape xmlns:android=”http://schemas.android.com/apk/res/android” >
<gradient android:angle=”270″ android:endColor=”#1D62F0″ android:startColor=”#1AD6FD” />
<corners android:radius=”10dp” />
</shape>
|
GUI-ch0108_bg_button_pressed
<?xml version=”1.0″ encoding=”utf-8″?> <shape xmlns:android=”http://schemas.android.com/apk/res/android” >
<gradient android:angle=”270″ android:endColor=”#FF2A68″ android:startColor=”#FF5E3A” />
<corners android:radius=”10dp” />
</shape>
|