Android知识点五:底部菜单效果(方式2)

       首先是activity_mian.xml(首页布局)

    

<?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="fill_parent"
            android:layout_weight="1.0" >
        </FrameLayout>

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:visibility="gone" />

        <RadioGroup
            android:id="@+id/main_tab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="@drawable/bg_menu"
            android:gravity="center_vertical"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/rg_home"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/main_home_background_selector"
                android:button="@null"
                android:checked="true"
                android:drawableTop="@drawable/bg_ico_home_selector"
                android:gravity="center_horizontal"
                android:paddingTop="8dip"
                android:tag="0"
                android:text="@string/rg_btn_home"
                android:textColor="@color/guid_textcolor_selector"
                android:textSize="10sp" />

            <RadioButton
                android:id="@+id/rg_chat"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/main_home_background_selector"
                android:button="@null"
                android:drawableTop="@drawable/bg_ico_chat_selector"
                android:gravity="center_horizontal"
                android:paddingTop="8dip"
                android:tag="1"
                android:text="@string/rg_btn_chat"
                android:textColor="@color/guid_textcolor_selector"
                android:textSize="10sp" />

            <RadioButton
                android:id="@+id/rg_shopping"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/main_home_background_selector"
                android:button="@null"
                android:drawableTop="@drawable/bg_ico_shopping_selector"
                android:gravity="center_horizontal"
                android:paddingTop="8dip"
                android:tag="2"
                android:text="@string/rg_btn_shopping"
                android:textColor="@color/guid_textcolor_selector"
                android:textSize="10sp" />

            <RadioButton
                android:id="@+id/rg_user"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:background="@drawable/main_home_background_selector"
                android:button="@null"
                android:drawableTop="@drawable/bg_ico_user_selector"
                android:gravity="center_horizontal"
                android:paddingTop="8dip"
                android:tag="3"
                android:text="@string/rg_btn_user"
                android:textColor="@color/guid_textcolor_selector"
                android:textSize="10sp" />
        </RadioGroup>
    </LinearLayout>

</TabHost>

MainActivity.java代码:

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();
		initListener();
	}

	void initView() {
		// 实例化TabHost
		tabhost = this.getTabHost();
		tabhost.addTab(tabhost.newTabSpec("ONE").setIndicator("ONE")
				.setContent(new Intent(this, HomeActivity.class)));
		tabhost.addTab(tabhost.newTabSpec("TWO").setIndicator("TWO")
				.setContent(new Intent(this, FormLogin.class)));
		tabhost.addTab(tabhost.newTabSpec("THREE").setIndicator("THREE")
				.setContent(new Intent(this, ShoppingCarActivity.class)));
		tabhost.addTab(tabhost.newTabSpec("FOUR").setIndicator("FOUR")
				.setContent(new Intent(this, IndividualCenterActivity.class)));

		mRadioGroup = (RadioGroup) findViewById(R.id.main_tab);
	}

	void initListener() {
		for (int i = 0; i < mRadioGroup.getChildCount(); i++) {
			final View v = mRadioGroup.getChildAt(i);
			if (v instanceof RadioButton) {
				((RadioButton) v)
						.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
							@Override
							public void onCheckedChanged(
									CompoundButton buttonView, boolean isChecked) {
								int page = Integer.parseInt(buttonView.getTag()
										.toString());
								if (isChecked) {
									switch (page) {
									case 0:// 首页
										tabhost.setCurrentTabByTag("ONE");
										break;
									case 1:// 聊吧
										tabhost.setCurrentTabByTag("TWO");
										break;
									case 2:// 购物车
										tabhost.setCurrentTabByTag("THREE");
										break;
									case 3:// 个人中心
										tabhost.setCurrentTabByTag("FOUR");
										break;
									default:
										break;
									}

								}
							}

							public void onCheckedChanged(RadioGroup group,
									int checkedId) {
								// TODO Auto-generated method stub

							}
						});
			}
		}
	}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值