先分析上图:用的是TabActivity,最底下是切换activity的标签,标签以上的view是个activity,当点击某个标签时,上面的view切换到指定的activity。
用法:首先写一个activity继承TabActivity
在onCreat()中调用以下方法
private void initView() {
mTabButtonGroup = (RadioGroup) findViewById(R.id.home_radio_button_group);
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);
Intent i_cart = new Intent(this, CartActivity.class);
Intent i_personal = new Intent(this, PersonalActivity.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.addTab(mTabHost.newTabSpec(TAB_CART).setIndicator(TAB_CART)
.setContent(i_cart));
mTabHost.addTab(mTabHost.newTabSpec(TAB_PERSONAL)
.setIndicator(TAB_PERSONAL).setContent(i_personal));
mTabHost.setCurrentTabByTag(TAB_MAIN);
mTabButtonGroup
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.home_tab_main:
mTabHost.setCurrentTabByTag(TAB_MAIN);
break;
case R.id.home_tab_search:
mTabHost.setCurrentTabByTag(TAB_SEARCH);
break;
case R.id.home_tab_category:
mTabHost.setCurrentTabByTag(TAB_CATEGORY);
break;
case R.id.home_tab_cart:
mTabHost.setCurrentTabByTag(TAB_CART);
break;
case R.id.home_tab_personal:
mTabHost.setCurrentTabByTag(TAB_PERSONAL);
break;
default:
break;
}
}
});
}
布局文件必须按以下格式书写:因为这个布局是要提供给tabActivity的,注意下id就知道,id是使用android提供的,而不是自己写的!!!
<?xml version="1.0" encoding="UTF-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0"
android:background="@drawable/android_layout_bg" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:visibility="gone" />
<RadioGroup
android:id="@+id/home_radio_button_group"
android:layout_width="match_parent"
android:layout_height="45dip"
android:layout_gravity="bottom"
android:background="@color/white"
android:gravity="center_vertical"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/home_tab_main"
style="@style/home_tab_bottom"
android:text="@string/title_index"
android:checked="true"
android:drawableTop="@drawable/index_indicator_bg" />
<RadioButton
android:id="@+id/home_tab_search"
style="@style/home_tab_bottom"
android:text="@string/title_category"
android:drawableTop="@drawable/category_indicator_bg" />
<RadioButton
android:id="@+id/home_tab_category"
style="@style/home_tab_bottom"
android:text="@string/title_stars"
android:drawableTop="@drawable/stars_indicator_bg" />
<RadioButton
android:id="@+id/home_tab_cart"
style="@style/home_tab_bottom"
android:text="@string/title_cart"
android:drawableTop="@drawable/cart_indicator_bg" />
<RadioButton
android:id="@+id/home_tab_personal"
style="@style/home_tab_bottom"
android:text="@string/title_my"
android:drawableTop="@drawable/my_indicator" />
</RadioGroup>
</LinearLayout>
</TabHost>