<转>tabHost研究二——利用Radiobutton嵌套在RadioGroup实现TabHost效果

MainActivity.java

  1. public class MainActivity extends TabActivity {
  2.         private RadioGroup group;
  3.         private TabHost tabHost;
  4.         public static final String TAB_HOME = "tabHome";
  5.         public static final String TAB_MES = "tabMes";
  6.         public static final String TAB_TOUCH = "tab_touch";
  7.         @Override
  8.         protected void onCreate(Bundle savedInstanceState) {
  9.                 // TODO Auto-generated method stub
  10.                 super.onCreate(savedInstanceState);
  11.                 setContentView(R.layout.maintabs);
  12.                 group = (RadioGroup) findViewById(R.id.main_radio);
  13.                 tabHost = getTabHost();
  14.                 tabHost.addTab(tabHost.newTabSpec(TAB_HOME).setIndicator(TAB_HOME)
  15.                                 .setContent(new Intent(this, Main.class)));
  16.                 tabHost.addTab(tabHost.newTabSpec(TAB_MES).setIndicator(TAB_MES)
  17.                                 .setContent(new Intent(this, Main2.class)));
  18.                 tabHost.addTab(tabHost.newTabSpec(TAB_TOUCH).setIndicator(TAB_TOUCH)
  19.                                 .setContent(new Intent(this, Main.class)));
  20.                 group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  21.                         public void onCheckedChanged(RadioGroup group, int checkedId) {
  22.                                 switch (checkedId) {
  23.                                 case R.id.radio_button0:
  24.                                         tabHost.setCurrentTabByTag(TAB_HOME);
  25.                                         break;
  26.                                 case R.id.radio_button1:
  27.                                         tabHost.setCurrentTabByTag(TAB_MES);
  28.                                         break;
  29.                                 case R.id.radio_button2:
  30.                                         tabHost.setCurrentTabByTag(TAB_TOUCH);
  31.                                         break;
  32.                                 default:
  33.                                         break;
  34.                                 }
  35.                         }
  36.                 });
  37.         }
  38. }

maintabs.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent"
  3.         android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
  4.         <LinearLayout android:orientation="vertical"
  5.                 android:layout_width="fill_parent" android:layout_height="fill_parent">
  6.                 <FrameLayout android:id="@android:id/tabcontent"
  7.                         android:layout_width="fill_parent" android:layout_height="0.0dip"
  8.                         android:layout_weight="1.0" />
  9.                 <TabWidget android:id="@android:id/tabs" android:visibility="gone"
  10.                         android:layout_width="fill_parent" android:layout_height="wrap_content"
  11.                         android:layout_weight="0.0" />
  12.                 <RadioGroup android:gravity="center_vertical"
  13.                         android:layout_gravity="bottom" android:orientation="horizontal"
  14.                         android:id="@+id/main_radio" android:background="@drawable/home_btn_bg"
  15.                         android:layout_width="fill_parent" android:layout_height="wrap_content">
  16.                         <RadioButton android:id="@+id/radio_button0" android:tag="radio_button0"
  17.                                 android:layout_marginTop="2.0dip" android:text="@string/main_home" android:background="@drawable/home_btn_bg"
  18.                                 android:drawableTop="@drawable/icon_1_n" style="@style/main_tab_bottom" />
  19.                         <RadioButton android:id="@+id/radio_button1" android:tag="radio_button1"
  20.                                 android:layout_marginTop="2.0dip" android:text="@string/main_message" android:background="@drawable/home_btn_bg"
  21.                                 android:drawableTop="@drawable/icon_2_n" style="@style/main_tab_bottom" />
  22.                         <RadioButton android:id="@+id/radio_button2" android:tag="radio_button2"
  23.                                 android:layout_marginTop="2.0dip" android:text="@string/main_comment"  android:background="@drawable/home_btn_bg"
  24.                                 android:drawableTop="@drawable/icon_3_n" style="@style/main_tab_bottom" />
  25.                 </RadioGroup>
  26.         </LinearLayout>
  27. </TabHost>

最关键就是上面这个XML了。它调用系统的tabhost

main.java

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.         android:orientation="vertical" android:layout_width="fill_parent"
  4.         android:layout_height="fill_parent">
  5.         
  6.         <ImageView android:text="@+id/Button01" android:id="@+id/Button01"
  7.                 android:layout_width="wrap_content" android:layout_height="wrap_content"
  8.                 android:background="@drawable/sfsd"></ImageView>
  9.                 
  10. </LinearLayout>
  1. public class Main2 extends Activity{
  2.         @Override
  3.         protected void onCreate(Bundle savedInstanceState) {
  4.                 // TODO Auto-generated method stub
  5.                 super.onCreate(savedInstanceState);
  6.                 setContentView(R.layout.main2);
  7.                 
  8.         }
  9. }
  1. public class Main2 extends Activity{
  2.         @Override
  3.         protected void onCreate(Bundle savedInstanceState) {
  4.                 // TODO Auto-generated method stub
  5.                 super.onCreate(savedInstanceState);
  6.                 setContentView(R.layout.main2);
  7.                 
  8.         }
  9. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.         <ImageView android:text="@+id/Button01" android:id="@+id/Button01"
  8.                 android:layout_width="wrap_content" android:layout_height="wrap_content"
  9.                 android:background="@drawable/dsfdsfds"></ImageView>
  10. </LinearLayout>
posted @ 2011-10-21 10:25  囧里个囧  阅读(2632)  评论(1)    收藏  举报