一个Android应用的好坏可能对于我们开发人员来说主要考虑其运行效率和性能,可是对于用户来说,一般情况下他们注重的是应用的外表,也就是时一个应用能否成功的
让各大用户青睐,其应用的的外貌起着至关重要的觉得。因此,今天我们来探讨一下如何使我们的应用中的按钮更加美观,让用户的体验效果更好。
Button按钮在应用中是很广泛的,使用起来也比较容易,可以通过为按钮制定android:background属性为按钮增加背景颜色或背景图片,如果将背景图片设为不规则的背景图片,
则可以开发出各种不规则形状的按钮。
如果只是使用普通的按钮背景颜色或背景图片,那么这些背景是固定的,不会随着用户的动作而改变。如果需要让按钮的背景颜色,背景图片随着用户动作动态改变,则可以考虑使用自定义Drawable对象来实现。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" android:layout_margin="10dp"> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="12sp" android:text="系统默认的按钮" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="12sp" android:text="有背景颜色的系统按钮" android:background="@drawable/blue"/> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/blue2" android:layout_marginTop="10dp" android:textSize="12sp" android:text="drawable9patch工具处理后有背景色按钮" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <!-- android:state_pressed按钮被按下,android:drawable当前状态显示的图片 --> <item android:state_pressed="true" android:drawable="@drawable/blue2"/> <!--默认的效果 --> <item android:drawable="@drawable/black2"></item> </selector>
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="red">#ff0000</color> <color name="green">#00ff00</color> </resources>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 矩形的背景色 --> <solid android:color="@color/green"/> <!-- 圆角的大小 --> <corners android:radius="5dp"/> </shape>