RadioGroup+TabHost组合实现底部导航

首先是布局文件

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/tabhost"
    >

    <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="0dp"
            android:layout_weight="1"
            ></FrameLayout>

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

        <!-- 视觉上,用单选按钮替代TabWidget -->
        <RadioGroup
            android:id="@+id/rg_main"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:background="@color/radio_button_unselected_color"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="4" > //表示子view所有的权重和不大于4

            <!--//android:drawableTop="@drawable/tab_home_bg" //表示图片在上部,这里也是一个状态选择器-->
            <RadioButton
                android:id="@+id/rb_home"
                android:gravity="center"
                android:button="@null"
                android:drawableTop="@drawable/zhuye_bg"
                android:textColor="@drawable/text_bg"
                android:layout_height="match_parent"
                android:text="主页"
                android:textSize="13sp"
                android:layout_weight="1" />
            <RadioButton
                android:id="@+id/rb_ask"
                android:gravity="center"
                android:layout_weight="1"
                android:button="@null"
                android:textSize="13sp"
                android:drawableTop="@drawable/chanpin_bg"
                android:textColor="@drawable/text_bg"
                android:layout_height="match_parent"
                android:text="咨询" />
            <RadioButton
                android:id="@+id/rb_chat"
                android:gravity="center"
                android:layout_weight="1"
                android:button="@null"
                android:textSize="13sp"
                android:drawableTop="@drawable/kefu_bg"
                android:textColor="@drawable/text_bg"
                android:layout_height="match_parent"
                android:text="会话" />
            <RadioButton
                android:id="@+id/rb_user"
                android:gravity="center"
                android:layout_weight="1"
                android:button="@null"
                android:textSize="13sp"
                android:drawableTop="@drawable/geren_bg"
                android:layout_height="match_parent"
                android:textColor="@drawable/text_bg"
                android:text="我的" />
        </RadioGroup>
    </LinearLayout>
</TabHost>

接下来Activity中的代码:

/**
 * Created by Liu on 2016/9/20.
 * 添加底部导航,使用RadioGroup+TabHost组合的方式实现
 */
public class ReplaceActivity extends TabActivity {
    //初始化TabHostRadioGroupRadioButton
    private TabHost mTabHost;
    private RadioGroup main_radiogroup;
    private RadioButton tab_icon_main, tab_icon_ask, tab_icon_chat, tab_icon_user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.bottom_guide);
        final Intent intent = getIntent();


        //获取按钮
        main_radiogroup = (RadioGroup) findViewById(R.id.rg_main);

        tab_icon_user = (RadioButton) findViewById(R.id.rb_user);
        tab_icon_ask = (RadioButton) findViewById(R.id.rb_ask);
        tab_icon_chat = (RadioButton) findViewById(R.id.rb_chat);
        tab_icon_main = (RadioButton) findViewById(R.id.rb_home);
        //TabWidget添加Tab
        mTabHost = getTabHost();
        mTabHost.addTab(mTabHost.newTabSpec("tag1").setIndicator("0").setContent(new Intent(this, MainActivity.class)));
        mTabHost.addTab(mTabHost.newTabSpec("tag2").setIndicator("1").setContent(new Intent(this, AskActivity.class)));
        mTabHost.addTab(mTabHost.newTabSpec("tag3").setIndicator("2").setContent(new Intent(this, ChatActivity.class)));
        mTabHost.addTab(mTabHost.newTabSpec("tag4").setIndicator("3").setContent(new Intent(this, UserActivity.class)));

        mTabHost.setCurrentTabByTag("tag1");
        tab_icon_main.setChecked(true);
        //设置监听事件
        checkListener checkradio = new checkListener();
        main_radiogroup.setOnCheckedChangeListener(checkradio);


    }
    //监听类
    class checkListener implements RadioGroup.OnCheckedChangeListener {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            //setCurrentTab 通过标签索引设置当前显示的内容
            //setCurrentTabByTag 通过标签名设置当前显示的内容

            switch (checkedId) {
                case R.id.rb_home:
                    mTabHost.setCurrentTab(0);
                    //                    //tabhost.setCurrentTabByTag("tag1");
                    break;
                case R.id.rb_ask:
                    mTabHost.setCurrentTab(1);
                    break;
                case R.id.rb_chat:
                    mTabHost.setCurrentTab(2);
                    break;
                case R.id.rb_user:
                    mTabHost.setCurrentTab(3);
                    break;
            }


        }
    }
}

当然,状态选择器的代码在这里就不写了

再贴一张效果图:


一共就这么多了,不足之处还望大家指正!

另外奉上一篇永祥大神的博客,大家可以作为参考,链接如下:

http://blog.csdn.net/u014452224/article/details/51742124

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值