1. 程式人生 > >Android自己定義矩形及selector、shape的使用

Android自己定義矩形及selector、shape的使用

文字 nor div wrap jsb 對話框 lin img get

【聲明】轉載請註明出處。此文出自指尖飛落的博客:http://blog.csdn.net/huntersnail

——每天寫一篇博客。每天做一點技術積累!


Android自己定義矩形及selector、shape的使用

因為項目開發須要,曾經盡管用過selector、shape可是都沒有好好去研究過,僅僅知道用。不知道它們的屬性詳細有哪些作用。

盡管網上一查就都知道了,感覺還是要自己去弄懂一下。

以下咱們一起去研究下:


一、xml布局文件

/測試Demo/res/layout/check_box.xml

<?

xml version="1.0" encoding="utf-8"?

> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/order_meth" style="@style/style_padding" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="@drawable/selector_mine" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="送達方式" android:textSize="18sp" /> <RadioGroup android:id="@+id/type" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal" > <RadioButton android:id="@+id/rb_one" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/sku_rd" android:layout_marginLeft="20dp" android:gravity="center" android:checked="true" android:padding="10dp" android:text="一小時達" /> <RadioButton android:id="@+id/rb_two" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/sku_rd" android:layout_marginLeft="10dp" android:gravity="center" android:padding="10dp" android:text="預定送達" /> </RadioGroup> </LinearLayout> </LinearLayout>


二、點擊時背景顏色處理

1、style樣式

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="sku_rd">
        <item name="android:textColor">@color/selector_text</item>
        <item name="android:background">@drawable/check_boxs</item>
        <item name="android:button">@null</item><!--去掉RadioButton的那個圓形-->
    </style>
</resources>


2、點擊RadioButton邊框背景顏色變換的關鍵xml

屬性解讀

“true”表示按下狀態使用(比如button按下)。“false”表示非按下狀態使用。


android:state_focused="true/false"

“true”表示聚焦狀態使用(比如使用滾動球/D-pad聚焦Button);“false”表示非聚焦狀態使用。
android:state_selected="true/false"

“true”表示選中狀態使用(比如Tab打開);“false”表示非選中狀態使用。
android:state_active="true/false"

“true”表示可勾選狀態時使用;“false”表示非可勾選狀態使用。

(僅僅對能切換可勾選—非可勾選的構件實用。)
android:state_checkable="true/false"

“true”表示勾選狀態使用。“false”表示非勾選狀態使用。
android:state_checked="true/false"

true”表示勾選狀態使用;“false”表示非勾選狀態使用。


android:state_enabled="true/false"

“true”表示可用狀態使用(能接收觸摸/點擊事件)。“false”表示不可用狀態使用。


android:state_window_focused="true/false"

“true”表示應用程序窗體有焦點時使用(應用程序在前臺);“false”表示無焦點時使用(比如Notification欄拉下或對話框顯示)。


/測試Demo/res/drawable/check_boxs.xml

<?

xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_checked="true" android:drawable="@drawable/shape_sku"></item> <item android:state_pressed="true" android:drawable="@drawable/shape_sku"></item> <item android:state_selected="true" android:drawable="@drawable/shape_sku"></item> <item android:drawable="@drawable/shape_sku_normal"></item> </selector>


3、文字顏色的變化

/測試Demo/res/color/selector_text.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 文字選中時的背景-->
    <item android:state_focused="true" android:color="#DB251F"/>
    <!-- 文字單擊時的背景 -->
    <item android:state_pressed="true" android:color="#DB251F"/>
    <item android:state_checked="true" android:color="#DB251F"/>
    <item android:state_selected="true" android:color="#DB251F"/>
    <!-- 文字默認的背景 -->
    <item android:color="#969696"/>
</selector>


三、以下是自己定義的兩個矩形

屬性解讀

corners:圓角

android:radius為角的弧度,值越大角越圓。
我們還能夠把四個角設定成不同的角度。同一時候設置五個屬性。則Radius屬性無效
android:Radius="20dp" 設置四個角的半徑
android:topLeftRadius="20dp" 設置左上角的半徑
android:topRightRadius="20dp" 設置右上角的半徑
android:bottomLeftRadius="20dp" 設置右下角的半徑
android:bottomRightRadius="20dp" 設置左下角的半徑


stroke:描邊
android:width="2dp" 描邊的寬度

android:color 描邊的顏色。


我們還能夠把描邊弄成虛線的形式,設置方式為:
android:dashWidth="5dp"
android:dashGap="3dp"
當中android:dashWidth表示‘-‘這樣一個橫線的寬度,android:dashGap表示之間隔開的距離。


solid:填充
android:color指定填充的顏色


/測試Demo/res/drawable/shape_sku_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <stroke android:width="1dp" android:color="#C8C8C8"/>
    <corners android:radius="3dp"/>
    <solid android:color="@android:color/white"/>
</shape>
/測試Demo/res/drawable/shape_sku.xml
<?

xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <stroke android:width="1dp" android:color="#DB251F"/> <corners android:radius="3dp"/> <solid android:color="@android:color/white"/> </shape>


四、效果圖

技術分享

五、源代碼地址點擊打開鏈接


☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆轉載請註明出處?指尖飛落的博客☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆

Android自己定義矩形及selector、shape的使用