Android创建底部菜单栏

布局

使用RadioGroup和RadioButton配合Fragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ado.video.TabMenuActivity">
    <!--Fragment容器-->
    <FrameLayout
        android:id="@+id/home_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!--底部tab栏-->
    <RadioGroup
        android:id="@+id/home_tab"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_alignParentBottom="true"
        android:background="#2da174"
        android:orientation="horizontal">
        <!--为每个tab的图片和文字设置选择器-->
        <RadioButton
            android:id="@+id/tab_home"
            style="@style/HomeTabButton"
            android:drawableTop="@drawable/selector_tab_home"
            android:text="home" />

        <RadioButton
            android:id="@+id/tab_sports"
            style="@style/HomeTabButton"
            android:drawableTop="@drawable/selector_tab_sport"
            android:text="sports" />

        <RadioButton
            android:id="@+id/tab_profile"
            style="@style/HomeTabButton"
            android:drawableTop="@drawable/selector_tab_profile"
            android:text="profile" />
    </RadioGroup>
</RelativeLayout>
RadioButton的style
     <style name="HomeTabButton">
        <item name="android:layout_height">match_parent</item>
        <item name="android:layout_weight">1</item>
        <item name="android:gravity">center</item>
        <item name="android:button">@null</item>
        <item name="android:textSize">17sp</item>
        <item name="android:textColor">@drawable/selector_tab_text</item>
    </style>

Activity代码

public class TabMenuActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    @BindView(R.id.home_container)
    FrameLayout homeContainer;
    @BindView(R.id.tab_home)
    RadioButton tabHome;
    @BindView(R.id.tab_sports)
    RadioButton tabSports;
    @BindView(R.id.tab_profile)
    RadioButton tabProfile;
    @BindView(R.id.home_tab)
    RadioGroup homeTab;


    private OneFragment oneFragment;
    private TwoFragment twoFragment;
    private ThreeFragment threeFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_menu);
        ButterKnife.bind(this);

        homeTab.setOnCheckedChangeListener(this);
        //默认选中首页
        //必须先监听了,再设置才有效
        homeTab.check(R.id.tab_home);
    }

    /**
     * 隐藏所有Fragment
     *
     * @param transaction
     */
    private void hideAllFragments(FragmentTransaction transaction) {
        if (oneFragment != null) {
            transaction.hide(oneFragment);
        }
        if (twoFragment != null) {
            transaction.hide(twoFragment);
        }
        if (threeFragment != null) {
            transaction.hide(threeFragment);
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideAllFragments(transaction);
        //每个tab选中前先隐藏,否则会出现重叠状态
        //tab没有出现过才创建,有了就直接通过show()显示
        switch (i) {
            case R.id.tab_home:
                if (oneFragment == null) {
                    oneFragment = new OneFragment();
                    transaction.add(R.id.home_container, oneFragment);
                } else {
                    transaction.show(oneFragment);
                }
                break;
            case R.id.tab_sports:
                if (twoFragment == null) {
                    twoFragment = new TwoFragment();
                    transaction.add(R.id.home_container, twoFragment);
                } else {
                    transaction.show(twoFragment);
                }
                break;
            case R.id.tab_profile:
                if (threeFragment == null) {
                    threeFragment = new ThreeFragment();
                    transaction.add(R.id.home_container, threeFragment);
                } else {
                    transaction.show(threeFragment);
                }
                break;
        }
        //最后记得提交了才生效
        transaction.commit();
    }
}

猜你喜欢

转载自blog.csdn.net/adojayfan/article/details/53277097