Android创建底部菜单栏

布局

使用RadioGroup和RadioButton配合Fragment


<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">
    
    <FrameLayout
        android:id="@+id/home_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <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">
        
        <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_parentitem>
        <item name="android:layout_weight">1item>
        <item name="android:gravity">centeritem>
        <item name="android:button">@null
        "android:textSize">17sp
        "android:textColor">@drawable/selector_tab_text
    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();
    }
}

你可能感兴趣的:(Android学习,tab,radiogroup,radio,button,tab菜单栏)