自定義:android
設置一個selector給button屬性(給RadioButton設置):code
android:button="@drawable/radiobutton_selector"
selector以下:xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/ic_select_yes" android:state_checked="true" android:state_enabled="true"/> <item android:drawable="@drawable/ic_select_no" android:state_checked="false" android:state_enabled="true"/> </selector>
這樣設置好以後樣式就ok了,但此時會出現文字與圖片間距太近的問題,那麼這時候在設置幾個屬性(給RadioButton設置)就能夠調整距離了,代碼以下:圖片
android:background="@null" android:button="@drawable/radiobutton_selector" android:drawableLeft="@drawable/radiobutton_selector" android:drawablePadding="5dp"
下面是一個RadioGroup的完整代碼(固然這裏只有xml代碼):utf-8
<RadioGroup android:id="@+id/radio_group" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginRight="18dp" android:gravity="left|center_vertical" android:orientation="horizontal" > <RadioButton android:id="@+id/radio_one" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginRight="18dp" android:background="@null" android:button="@drawable/radiobutton_selector" android:drawableLeft="@drawable/radiobutton_selector" android:drawablePadding="5dp" android:text="@string/net_wired" android:textColor="@color/text_color_black" android:textSize="15sp" /> <RadioButton android:id="@+id/radio_two" android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@null" android:button="@drawable/radiobutton_selector" android:drawableLeft="@drawable/radiobutton_selector" android:drawablePadding="5dp" android:text="@string/net_wifi" android:textColor="@color/text_color_black" android:textSize="15sp" /> </RadioGroup>