android > 按钮Button 按下效果
************** 按下 更改 背景颜色 ********************
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/btn_corner_white" /> <item android:state_focused="true" android:drawable="@drawable/btn_corner_white" /> <item> <shape> <solid android:color="#CE0525"/><!--可以作为整个背景的颜色--> <corners android:radius="3px" /> </shape> </item> </selector>
*************** 带图片的 *******
先是准备 背景素材,命名规则为
***_normal.9.png 正常状况
***_pressed.9.png 按下状况
附近附带若干窗口图片
然后 drawable 新建 样式 xml
btn_style_alert_dialog_button.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/btn_style_alert_dialog_special_pressed" /> <item android:drawable="@drawable/btn_style_alert_dialog_special_normal" /> </selector>
可圆角样式
<?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:angle="270" android:endColor="#99BD4C" android:startColor="#A5D245" /> <corners android:radius="8dp" /> </shape> </item> <item> <shape> <gradient android:angle="270" android:endColor="#99BD4C" android:startColor="#A5D245" /> <corners android:radius="8dp" /> </shape> </item> </selector>
<?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:angle="0" android:endColor="@color/self_dark" android:startColor="@color/self_dark" /> <corners android:radius="8dp" /> </shape> </item> <item> <shape> <gradient android:angle="0" android:endColor="@color/self_dark_light" android:startColor="@color/self_dark_light" /> <corners android:radius="8dp" /> </shape> </item> </selector>
如果要增加边框 在 shape 里 加 <stroke android:width="1.0dip" android:color="@color/self_dark" />
其中@color 在res/values/strings.xml
<color name="self_dark">#2D3238</color> <color name="self_dark_light">#3C4049</color>
然后在 Button 中的background 中 加载 样式
<?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:orientation="vertical" > <Button android:id="@+id/btn_take_photo" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="20dip" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="拍照" android:background="@drawable/btn_style_alert_dialog_button" android:textStyle="bold" /> </LinearLayout>
** android 按钮按下时改变字体颜色
1.新建 drawable/button_font_style.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#fff"/>
<item android:color="#000" />
</selector>
2.添加至需要的xml里
<android:textColor="@drawable/button_font_style">