Android 自訂RadioButton樣式

來源:互聯網
上載者:User

標籤:and   接下來   select   abs   text   如何   ddr   xmlns   建立   

 
上面這種3選1的效果如何做呢?用代碼寫? 
其實有更簡單的辦法,忘了RadioButton有什麼特性了嗎? 
我就用RadioButton實現了如上效果,其實很簡單的。 
首先定義一張background,命名為radio.xml,注意該background必須為xml樣式的圖片: 

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

這裡我們只要關心state_checked狀態就可以了。所以很簡單的配置。 
接下來就是布局檔案裡面引用這張圖片了:

<RadioGroup          android:gravity="center"          android:orientation="horizontal"            android:layout_width="fill_parent"             android:layout_height="wrap_content"          >          <RadioButton                 android:id="@+id/btn_0"                android:layout_width="fill_parent"                 android:layout_height="wrap_content"              android:text="最新資訊"               android:textSize="17.0sp"                 android:textColor="@android:color/black"                android:gravity="center"               android:layout_weight="1"              android:checked="true"              android:button="@null"              android:background="@drawable/radio"              />            <RadioButton                 android:id="@+id/btn_1"              android:layout_width="fill_parent"                 android:layout_height="wrap_content"              android:text="線上專家"              android:textSize="17.0sp"              android:textColor="@android:color/black"                android:gravity="center"              android:layout_weight="1"              android:button="@null"              android:background="@drawable/radio"              />            <RadioButton                 android:id="@+id/btn_2"              android:layout_width="fill_parent"                 android:layout_height="wrap_content"              android:text="預約服務"              android:textSize="17.0sp"              android:textColor="@android:color/black"                android:gravity="center"              android:layout_weight="1"              android:button="@null"              android:background="@drawable/radio"              />        </RadioGroup>  

注意將android:button="@null"全部設為null,並且將android:background="@drawable/radio"設為剛才建立的圖片。這樣就完成了。 
一句代碼都不用寫! 
看看豎下來的效果: 
 
傍邊有表徵圖的怎麼辦? 
 
也很簡單啦,只要在每個RadioButton上加android:drawableLeft="@drawable/tubiao_0"就可以了。 
另外要設定表徵圖與文字的距離怎麼辦? 
有一個方法setCompoundDrawablePadding(pad)可以設定表徵圖與文字的距離,對應的屬性為android:drawablePadding。 
有了這招完全可以實作類別似的n選1效果,看下面: 
 
 
應用: 
http://www.iteye.com/topic/1116261#2256664 

Android 自訂RadioButton樣式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.