/res/values/styles.xml代码:
<resources> <style name="main_tab_bottom"> <item name="android:textSize">12.0dip</item> <item name="android:textColor">#ffffffff</item> <item name="android:ellipsize">marquee</item> <item name="android:gravity">center_horizontal</item> <item name="android:paddingTop">5.0dip</item> <item name="android:layout_width">fill_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:button">@null</item> <item name="android:singleLine">true</item> <item name="android:drawablePadding">2.0dip</item> <item name="android:layout_weight">1.0</item> </style> </resources>
layout布局:
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/msg_title" android:layout_width="fill_parent" android:layout_height="wrap_content" android:visibility="gone" > <!-- VISIBLE:设置控件可见 INVISIBLE:设置控件不可见 GONE:设置控件隐藏 --> <TextView android:id="@+id/tv_wb" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/titlebar_lightgray_bg" > </TextView> </FrameLayout> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="0.0dip" android:layout_weight="1.0" /> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="0.0" android:visibility="gone" /> <RadioGroup android:id="@+id/radio" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="@drawable/maintab_toolbar_bg" android:gravity="center_vertical" android:orientation="horizontal" > <RadioButton android:id="@+id/radio_button0" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_1_n" android:tag="radio_button0" android:text="首页" style="@style/main_tab_bottom" /> <RadioButton android:id="@+id/radio_button1" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_2_n" android:tag="radio_button1" android:text="信息" style="@style/main_tab_bottom"/> <RadioButton android:id="@+id/radio_button2" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_3_n" android:tag="radio_button2" android:text="名片" style="@style/main_tab_bottom" /> <RadioButton android:id="@+id/radio_button3" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_4_n" android:tag="radio_button3" android:text="查询" style="@style/main_tab_bottom"/> <RadioButton android:id="@+id/radio_button4" android:layout_marginTop="2.0dip" android:drawableTop="@drawable/icon_5_n" android:tag="radio_button4" android:text="更多" style="@style/main_tab_bottom" /> </RadioGroup> </LinearLayout> </TabHost>
java代码:
/* * Android菜单之底部TabHost,布局文件main.xml */ public void setTabHost(){ tabHost=this.getTabHost(); TabSpec tabSpec1=tabHost.newTabSpec("TAB_1").setIndicator("TAB_1"); //tabSpec1.setContent(new Intent(MainActivity.this,Activity1.class));//第一个activity tabHost.addTab(tabSpec1); TabSpec tabSpec2=tabHost.newTabSpec("TAB_2").setIndicator("TAB_2"); //tabSpec2.setContent(new Intent(MainActivity.this,Activity2.class));//第二个activity tabHost.addTab(tabSpec2); TabSpec tabSpec3=tabHost.newTabSpec("TAB_3").setIndicator("TAB_3"); //tabSpec3.setContent(new Intent(MainActivity.this,Activity3.class));//第三个activity tabHost.addTab(tabSpec3); TabSpec tabSpec4=tabHost.newTabSpec("TAB_4").setIndicator("TAB_4"); //tabSpec4.setContent(new Intent(MainActivity.this,Activity4.class));//第四个activity tabHost.addTab(tabSpec4); TabSpec tabSpec5=tabHost.newTabSpec("TAB_5").setIndicator("TAB_5"); //tabSpec5.setContent(new Intent(MainActivity.this,Activity5.class));//第五个activity tabHost.addTab(tabSpec5); //查找控件 radioGroup=(RadioGroup) findViewById(R.id.radio); //为菜单按钮添加事件 radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub switch (checkedId) { case R.id.radio_button0: tabHost.setCurrentTabByTag("TAB_1"); Toast.makeText(context, "进入第一个界面", Toast.LENGTH_SHORT).show(); break; case R.id.radio_button1: tabHost.setCurrentTabByTag("TAB_2"); Toast.makeText(context, "进入第二个界面", Toast.LENGTH_SHORT).show(); break; case R.id.radio_button2: tabHost.setCurrentTabByTag("TAB_3"); Toast.makeText(context, "进入第三个界面", Toast.LENGTH_SHORT).show(); break; case R.id.radio_button3: tabHost.setCurrentTabByTag("TAB_4"); Toast.makeText(context, "进入第四个界面", Toast.LENGTH_SHORT).show(); break; case R.id.radio_button4: tabHost.setCurrentTabByTag("TAB_5"); Toast.makeText(context, "进入第五个界面", Toast.LENGTH_SHORT).show(); break; default: break; } } }); }
效果图: