首先是布局,因为每次只能选择一个选项,所以选择RadioGroup
<RadioGroup android:id="@+id/home_radio_button_group" android:layout_width="match_parent" android:layout_height="55dp" android:layout_gravity="bottom" android:background="@drawable/main_navigation_background" android:gravity="center_vertical" android:orientation="horizontal" > <RadioButton android:id="@+id/tab_main" style="@style/home_tab_bottom" android:background="@drawable/home_tab_background_selector" android:checked="true" android:drawableTop="@drawable/home_tab_main_selector" /> <RadioButton android:id="@+id/tab_search" style="@style/home_tab_bottom" android:background="@drawable/home_tab_background_selector" android:drawableTop="@drawable/home_tab_search_selector" /> <RadioButton android:id="@+id/tab_category" style="@style/home_tab_bottom" android:background="@drawable/home_tab_background_selector" android:drawableTop="@drawable/home_tab_category_selector" /> </RadioGroup>java 代码实现-- extends TabActivity
public static final String TAB_MAIN = "MAIN_ACTIVITY"; public static final String TAB_SEARCH = "SEARCH_ACTIVITY"; public static final String TAB_CATEGORY = "CATEGORY_ACTIVITY";
mTabButtonGroup = (RadioGroup) findViewById(R.id.home_radio_button_group);
private TabHost mTabHost;mTabHost = getTabHost();
Intent i_main = new Intent(this, IndexActivity.class);//选择一个,则跳转一个界面 Intent i_search = new Intent(this, SearchActivity.class); Intent i_category = new Intent(this, CategoryActivity.class);mTabHost.addTab(mTabHost.newTabSpec(TAB_MAIN).setIndicator(TAB_MAIN) .setContent(i_main)); mTabHost.addTab(mTabHost.newTabSpec(TAB_SEARCH) .setIndicator(TAB_SEARCH).setContent(i_search)); mTabHost.addTab(mTabHost.newTabSpec(TAB_CATEGORY) .setIndicator(TAB_CATEGORY).setContent(i_category));mTabHost.setCurrentTabByTag(TAB_MAIN);//设置默认的界面,此时进入了第一个activitymTabButtonGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.home_tab_main: mTabHost.setCurrentTabByTag(TAB_MAIN); break;default: break; } } });