android中shape 的使用

來源:互聯網
上載者:User

標籤:

android 開發中 對於 shape 和 selector的使用,一直都不是很熟練, 記錄一下.便於以後參考.

舉個項目中例子圖

對於上面的2個radiobutton ,背景我們可以讓美工做一個.9圖來實現. 但是最後還是自己用shape 搞一下.

按鈕布局如下:

<LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="@dimen/space_8"        android:orientation="horizontal">        <RadioGroup            android:id="@+id/order_rg"            android:layout_width="match_parent"            android:layout_height="40dp"            android:layout_marginLeft="@dimen/space_8"            android:layout_marginRight="@dimen/space_8"            android:orientation="horizontal">       //預設選中            <RadioButton                android:id="@+id/rb_left"                android:layout_width="match_parent"                android:layout_height="35dp"                android:layout_weight="1"                android:background="@drawable/fg_order_rb_selector"                android:button="@android:color/transparent"                android:checked="true"                android:drawablePadding="@dimen/space_4"                android:gravity="center"                android:text="投資記錄"                android:textColor="@drawable/fg_order_text_selector"                android:textSize="@dimen/txt_14" />       //預設不選中            <RadioButton                android:id="@+id/rb_right"                android:layout_width="match_parent"                android:layout_height="35dp"                android:layout_weight="1"                android:background="@drawable/fg_order_rb_selector1"                android:button="@null"                android:drawablePadding="@dimen/space_4"                android:text="提現記錄"                android:gravity="center"                android:textColor="@drawable/fg_order_text_selector"                android:textSize="@dimen/txt_14" />        </RadioGroup>    </LinearLayout>

裡面用到2個 selector ,   1個是文字背景選取器fg_order_text_selector,1個RadioButton顏色背景選取器fg_order_rb_selector1

2個RadioButton用到的文字選取器如下:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:color="@color/white" android:state_checked="true"/> //白色    <item android:color="@color/black" android:state_checked="false"/>//黑色</selector>

第一個RadioButton用到的顏色背景選取器   :fg_order_rb_selector.xml  , rb_left.xml , rb_right.xml  如下:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:drawable="@drawable/rb_left"        android:state_checked="true"        android:state_enabled="true"        />    <item        android:drawable="@drawable/rb_right"        android:state_checked="false"        android:state_enabled="false"/></selector>
<shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <!-- 圓角 -->    <corners android:bottomLeftRadius="6dp" android:topLeftRadius="6dp" />    <stroke  android:color="@color/wave_bg" android:width="1dp" />    <!--內部填充色-->    <solid android:color="@color/wave_bg" /></shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <!-- 圓角 -->    <corners android:bottomLeftRadius="6dp" android:topLeftRadius="6dp" />    <stroke  android:color="@color/wave_bg" android:width="1dp" />    <!--內部填充色-->    <solid android:color="@color/white"/></shape>

第二個RadioButton用到的顏色背景選取器   :fg_order_rb_selector1.xml  , rb_left1.xml , rb_right1.xml  如下:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item        android:drawable="@drawable/rb_left1"        android:state_checked="true"        android:state_enabled="true"        />    <item        android:drawable="@drawable/rb_right1"        android:state_checked="false"        android:state_enabled="false"/></selector>
<shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <!-- 圓角 -->    <corners android:bottomRightRadius="6dp" android:topRightRadius="6dp" />    <stroke  android:color="@color/wave_bg" android:width="1dp" />    <!--內部填充色-->    <solid android:color="@color/wave_bg" /></shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle">    <!-- 圓角 -->    <corners android:bottomRightRadius="6dp" android:topRightRadius="6dp" />    <stroke  android:color="@color/wave_bg" android:width="1dp" />    <!--內部填充色-->    <solid android:color="@color/white"/></shape>

以上就是 全部資源檔了. 沒有用到任何一張圖.

參考博文:http://blog.csdn.net/zzy7075/article/details/42235947

android中shape 的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.