esdrawable adio_button_bg.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/raido_button_checked" android:state_checked="true"/> <item android:drawable="@android:color/transparent" /> </selector>
esvaluesstrings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">RadioButtonDemo</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="radio_button_1">JAVA</string> <string name="radio_button_2">ASP.NET</string> <string name="radio_button_3">P H P</string> </resources>
esvaluesstyles.xml
<style name="RadioButtonStyles"> <item name="android:layout_gravity">center_vertical</item> <item name="android:layout_height">fill_parent</item> <item name="android:layout_width">wrap_content</item> <item name="android:layout_weight">1</item> <item name="android:button">@null</item> <item name="android:background">@drawable/radio_button_bg</item> <item name="android:gravity">center</item> </style>
esmenuactivity_radio_button_demo.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_settings" android:orderInCategory="100" android:showAsAction="never" android:title="@string/menu_settings"/> </menu>
eslayoutactivity_radio_button_demo.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <RadioGroup android:id="@+id/radioGroup1" android:layout_width="fill_parent" android:layout_height="50dp" android:background="@drawable/x_proposal_tab_bg" android:orientation="horizontal" > <RadioButton android:id="@+id/radio0" style="@style/RadioButtonStyles" android:text="@string/radio_button_1" /> <RadioButton android:id="@+id/radio1" style="@style/RadioButtonStyles" android:text="@string/radio_button_2" /> <RadioButton android:id="@+id/radio2" style="@style/RadioButtonStyles" android:text="@string/radio_button_3" /> </RadioGroup> </RelativeLayout>
RadioButtonDemoActivity.java
public class RadioButtonDemoActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_radio_button_demo); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_radio_button_demo, menu); return true; } }