Skip to content

Instantly share code, notes, and snippets.

@tatuas
Created July 24, 2013 06:36
Show Gist options
  • Save tatuas/6068471 to your computer and use it in GitHub Desktop.
Save tatuas/6068471 to your computer and use it in GitHub Desktop.
Androidでオリジナルのラジオグループデザインをつくる方法。 my_radio_btn.xmlをres/drawableに作り、styles.xmlにstyleタグで追加。 activity_main.xmlなどで例のように宣言すればオリジナルラジオグループの完成。 サンプルはグラフィカルなiOS風になっている。 ラジオボタンのあの丸も消去してすっきりなサンプル。
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio_btn_1"
style="@style/MyRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/my_radio_btn"
android:checked="false"
android:text="1" />
<RadioButton
android:id="@+id/radio_btn_2"
style="@style/MyRadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|top"
android:background="@drawable/my_radio_btn"
android:checked="false"
android:text="2" />
</RadioGroup>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:shape="rectangle" android:state_checked="true">
<shape android:shape="rectangle">
<gradient android:angle="90" android:endColor="#444444" android:startColor="#ffffff" />
<stroke android:width="2dp" android:color="#333333" />
<padding android:bottom="10dip" android:left="10dip" android:right="10dip" android:top="10dip" />
<corners android:radius="5dip" />
</shape>
</item>
<item android:shape="rectangle" android:state_checked="false">
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<stroke android:width="1dp" android:color="#333333" />
<gradient android:angle="90" android:endColor="#ffffff" android:startColor="#444444" />
<padding android:bottom="10dip" android:left="10dip" android:right="10dip" android:top="10dip" />
<corners android:radius="1dip" />
</shape>
</item>
</selector>
<resources>
<style name="MyRadioButton" parent="android:Widget.CompoundButton.RadioButton">
<item name="android:button">@null</item>
</style>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment