TabActivity的使用

先分析上图:用的是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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值