在以前,我们直接就是在button的brackgroundd中编写drawable,button的点击图片,但是现在我们统一通过style来实现样式的设置,这样设置,更加规范化
步骤:
1.首先编写button的代码:
<Button android:id="@+id/btn_login" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登 录" android:layout_marginTop="30dp" android:layout_margin="20dp" style="@style/bigRedButton" />
2.看上面只有 style="@style/bigRedButton" ,这个样式没有定义,所以我们需要定义这个样式
在stytle中新建bigredButton的样式
<!--button的样式--> <style name="bigRedButton" > <item name="android:background">@drawable/bg_btn_style_red</item> </style>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_enabled="false"> <shape android:shape="rectangle"> <corners android:radius="@dimen/buttonCornerSize" /> <solid android:color="@color/red_btn_color_disable" /> </shape> </item> <item android:state_pressed="true"> <shape android:shape="rectangle"> <corners android:radius="@dimen/buttonCornerSize" /> <solid android:color="@color/red_btn_color_pressed" /> </shape> </item> <item> <shape android:shape="rectangle"> <corners android:radius="@dimen/buttonCornerSize" /> <solid android:color="@color/red_btn_color_normal" /> </shape> </item> </selector>
<resources> <!-- Default screen margins, per the Android Design guidelines. --> <dimen name="activity_horizontal_margin">16dp</dimen> <dimen name="activity_vertical_margin">16dp</dimen> <dimen name="buttonCornerSize">2.0dip</dimen> </resources>
最后就是我们的效果图: