Android之實現TextView控件圓角以及Button點擊、焦點效果

默認情況下,TextView是不帶邊框的,如果想要爲TextView添加邊框,只能考慮爲TextView設置一個背景Drawable。在Drawable文件夾下新建一個layout類型的xml文件,然後將Root tag改成shape,這樣xml文件也可以當Drawable使用。
bg_border.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <solid android:color="#0000"></solid>
    <stroke android:width="4px" android:color="#f00"></stroke>
    <corners android:topLeftRadius="20dp"
        android:topRightRadius="20dp"
        android:bottomLeftRadius="20dp"
        android:bottomRightRadius="20dp"></corners>
</shape>
最後再把TextView的android:background屬性設置成自定義的xml樣式文件就可以了:
android:background="@drawable/bg_border"
另外還可以設置漸變色、填充色等,如下:
gradient   -- 顏色漸變
        startcolor  起點顏色
        endcolor  終點顏色
        android:angle 角度  0是從左到右,90是從下到上
solid          --  填充
stroke        --  描邊 
corners      --  圓角 

padding     -- 內容離邊界的距離

同理,設置Button在不同狀態下的背景,也是使用在Drawable文件夾下添加xml文件,不過資源類型改爲selector。

selector.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/temp1" />
 <item android:state_pressed="false" android:state_focused="false"
  android:drawable="@drawable/temp2" />
 <item android:state_focused="true" android:drawable="@drawable/temp3" />
 <item android:state_focused="false" android:drawable="@drawable/temp4" />
</selector>

然後在button中設置如下:

<Button
 android:background="@drawable/selector"/>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章