TabHost 添加底部菜单

本文介绍了一种使用 Android 的 TabActivity 实现底部导航栏的方法。通过 TabHost 和 RadioGroup 构建了包含四个标签页(首页、分类、商城和个人中心)的应用界面,并详细展示了如何设置各个标签页的内容和切换监听。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<?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="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/page_bg" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <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/main_radio"
            android:layout_width="fill_parent"
            android:layout_height="65dp"
            android:layout_gravity="bottom"
            android:background="@drawable/main_tab_bg"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radio_button0"
                style="@style/main_tab_bottom"
                android:layout_marginBottom="3dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="3dp"
                android:checked="true"
                android:drawableTop="@drawable/main_jiong_icon"
                android:tag="radio_button0"
                android:text="首页" />

            <RadioButton
                android:id="@+id/radio_button1"
                style="@style/main_tab_bottom"
                android:layout_marginBottom="3dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="3dp"
                android:drawableTop="@drawable/main_shen_icon"
                android:tag="radio_button1"
                android:text="分类" />

            <RadioButton
                android:id="@+id/radio_button2"
                style="@style/main_tab_bottom"
                android:layout_marginBottom="3dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="3dp"
                android:drawableTop="@drawable/main_high_icon"
                android:tag="radio_button2"
                android:text="商城" />

            <RadioButton
                android:id="@+id/radio_button3"
                style="@style/main_tab_bottom"
                android:layout_marginBottom="3dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="3dp"
                android:drawableTop="@drawable/main_more_icon"
                android:tag="radio_button3"
                android:text="个人中心" />
        </RadioGroup>
    </LinearLayout>

</TabHost>



public class TabPageActivity extends TabActivity {
	public TabHost mth;
	public static final String TAB_ONE= "首页";
	public static final String TAB_TWO = "分类";
	public static final String TAB_THREE = "商城";
	public static final String TAB_FOUR  = "个人中心";
	public RadioGroup radioGroup;
	public static boolean unlock = false;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main); 
		
		this.initTableHost();

	}


	private void initTableHost() {
		mth = this.getTabHost();

		TabSpec ts1 = mth.newTabSpec(TAB_ONE).setIndicator(TAB_ONE);
		ts1.setContent(new Intent(TabPageActivity.this, HomePageActivity.class));
		mth.addTab(ts1);

		TabSpec ts2 = mth.newTabSpec(TAB_TWO).setIndicator(TAB_TWO);
		ts2.setContent(new Intent(TabPageActivity.this, FenLeiActivity.class));
		mth.addTab(ts2);
		
		TabSpec ts3 = mth.newTabSpec(TAB_THREE).setIndicator(TAB_THREE);
		Intent intent = new Intent(TabPageActivity.this,
				ShangchengWebViewActivity.class);
		intent.putExtra( Constants.WEB_VIEW_URL, "http://www.baidu.com");
		intent.putExtra(Constants.MENU_NAME, "商城");
		ts3.setContent(intent);
		mth.addTab(ts3);

		

		TabSpec ts4 = mth.newTabSpec(TAB_FOUR).setIndicator(TAB_FOUR);
		ts4.setContent(new Intent(TabPageActivity.this, MoreActivity.class));
		mth.addTab(ts4);

		this.radioGroup = (RadioGroup) findViewById(R.id.main_radio);
		mth.setCurrentTabByTag(TAB_ONE);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.radio_button0:
					mth.setCurrentTabByTag(TAB_ONE);
					break;
				case R.id.radio_button1:
					mth.setCurrentTabByTag(TAB_TWO);
					break;
				case R.id.radio_button2:
					mth.setCurrentTabByTag(TAB_THREE);
					break;
				case R.id.radio_button3:
					mth.setCurrentTabByTag(TAB_FOUR);
					break;

				}
			}
		});
		
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值