当需要为应用程序UI控件选择背景的时候,开发者会添加自定义的颜色和形状来代替系统的默认样式,

圆角边框看起来是很不错的效果,开发者只需要添加几行代码,就可以在应用程序中使用这种效果。

下面我们做一个例子看一看,新建一个main.xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:background="@drawable/button_rounded_background"
android:gravity="center_vertical"
android:padding="10dp"
android:text="Hello,RoundCornerDemo"
android:textColor="#FFFFFF" />

</LinearLayout>


其中我们为背景属性指定了drawable值,但是这个值并不是一个图片,而是一个xml文件。文件代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<solid android:color="#02A712" />

<corners android:radius="15dp" />

</shape>


仅仅只是两行代码。让我们看一下效果吧:

Android开发技巧四--圆角化控件,让它看起来更美_开发者