android:RadioButton自定義和非自定義的實現
阿新 • • 發佈:2018-12-30
我簡單解釋下:當root為null的時候,我們只是把一個xml檔案例項化成View物件,反回的就是xml對應的View.而當root不為null的時候,也就是存在parent.那麼我們將把這個xml例項化程View物件後,將這個View檢視add進其parent中.所以在這裡我們用的是LayoutInflater.from(context).inflate(R.layout.item,
this);這樣其實就是把XML例項化後當作自己的一部分,這樣我們在呼叫此控制元件的時候,顯示的就是我們想要的那個檢視了(XML檢視).說到這裡大家明白了.這樣以後用的時候再也不會糊塗了.如果想詳細瞭解那麼請參考這篇文章:View檢視框架原始碼分析之一:android是如何建立一個view.講解的那是的相當的透徹,看懂後對於以後我們開發是百利而無一害啊.(*^__^*)
.
/*****************************************************************************/
第二種方法:對RadioButton給定樣式進行改造 看配置檔案 seletor.xml and style.xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/radio_checked" android:state_checked="true" android:state_enabled="true"></item> <item android:drawable="@drawable/radio_unchecked" android:state_checked="false" android:state_enabled="true"></item> < /selector><resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="RadioButtonStyles"> <item name="android:button">@drawable/selector</item> </style> < /resources>最後只需要在RadioButton中引用即可. <RadioGroup android:id="@+id/rg_main" android:layout_width="match_parent" android:layout_height="wrap_content" > <RadioButton android:id="@+id/button1" style="@style/RadioButtonStyles" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="5K" /> <RadioButton android:id="@+id/button2" style="@style/RadioButtonStyles" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="10K" /> </RadioGroup>
這種方式很簡單吧,不過我覺的應用範圍沒有上面自定義來的廣,比如說上面我做的專案中,RadioButton中的text有兩項,這樣我們用RadioButton就無法實現了,遇到比較複雜的RadioButton選擇自定義是比較好的.
針對RadioButton 就說這麼多了,希望對你有幫助.
第二種方法:對RadioButton給定樣式進行改造 看配置檔案 seletor.xml and style.xml <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/radio_checked" android:state_checked="true" android:state_enabled="true"></item> <item android:drawable="@drawable/radio_unchecked" android:state_checked="false" android:state_enabled="true"></item> < /selector><resources xmlns:android="http://schemas.android.com/apk/res/android"> <style name="RadioButtonStyles"> <item name="android:button">@drawable/selector</item> </style> < /resources>最後只需要在RadioButton中引用即可. <RadioGroup android:id="@+id/rg_main" android:layout_width="match_parent" android:layout_height="wrap_content" > <RadioButton android:id="@+id/button1" style="@style/RadioButtonStyles" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="5K" /> <RadioButton android:id="@+id/button2" style="@style/RadioButtonStyles" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="10K" /> </RadioGroup>