Android基础——最详细的简单底部导航(RadioGroup+RadioButton)+FrameLayout联动

开发过程中,几乎每个项目都会使用到底部导航,这里简单的敲一个最基本的,用于方便开发

//大致效果截图


//主页面布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.somet.p2pinvest.activity.MainActivity">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/fl_main"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/home_back_selected"
            />
        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="54dp"
            android:orientation="horizontal"
            android:id="@+id/rg_main">

            <RadioButton
                android:id="@+id/rb_home"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:button="@null"
                android:drawableTop="@drawable/rb_home_top"
                android:gravity="center"
                android:text="首页"
                android:textColor="@drawable/rb_textcolor_blue" />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:button="@null"
                android:id="@+id/rb_invest"
                android:layout_weight="1"
                android:drawableTop="@drawable/rb_invest_top"
                android:text="投资"
                android:gravity="center"
                android:textColor="@drawable/rb_textcolor_blue"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:button="@null"
                android:id="@+id/rb_me"
                android:layout_weight="1"
                android:drawableTop="@drawable/rb_me_top"
                android:text="我的资产"
                android:gravity="center"
                android:textColor="@drawable/rb_textcolor_red"/>
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:button="@null"
                android:id="@+id/rb_more"
                android:layout_weight="1"
                android:drawableTop="@drawable/rb_more_top"
                android:text="更多"
                android:gravity="center"
                android:textColor="@drawable/rb_textcolor_blue"/>
        </RadioGroup>
    </LinearLayout>


</LinearLayout>

//drawableTop 这里放一个drawable文件,其他三个换图片即可

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bottom02" android:state_checked="true"/>
    <item android:drawable="@drawable/bottom01" android:state_checked="false"/>
</selector>

//这里使用了ButterKinfer 依赖来初始化控件

compile 'com.jakewharton:butterknife:8.8.1'
compile 'com.jakewharton:butterknife-compiler:8.8.1'

/**

*Android Studio3.0 可能会出现问题

*如果有问题: 则在 defaultConfig 中添加

*添加如下配置就OK

*javaCompileOptions { annotationProcessorOptions { includeCompileClasspath = true } }

*/

/**
 * 导入第三方库的三种方法:
 * 1.添加依赖
 * 2.导入jar包
 * 3.导入第三方model
 * */
public class MainActivity extends FragmentActivity {
    @BindView(R.id.fl_main)
    FrameLayout fl_main;
    @BindView(R.id.rg_main)
    RadioGroup rg_main;
    private List<Fragment> fragments;

    //创建四个Fragment
    private HomeFragment homeFragment;
    private InvesFragment invesFragment;
    private MeFragment meFragment;
    private MoreFragment moreFragment;
    private int selector;
    private FragmentTransaction transaction;
    private TextView zaigang;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initListener();



    }

    private void initListener() {
        rg_main.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentManager supportFragmentManager = getSupportFragmentManager();
                transaction = supportFragmentManager.beginTransaction();
                hideFragment();
                switch (checkedId){
                    case R.id.rb_home:
                        if (homeFragment == null){
                            homeFragment=new HomeFragment();
                            transaction.add(R.id.fl_main,homeFragment);
                        }
                        transaction.show(homeFragment);
                        break;
                    case R.id.rb_invest:
                        if (invesFragment == null){
                            invesFragment=new InvesFragment();
                            transaction.add(R.id.fl_main,invesFragment);
                        }
                        transaction.show(invesFragment);
                        break;
                    case R.id.rb_me:
                        if (meFragment == null){
                            meFragment=new MeFragment();
                            transaction.add(R.id.fl_main,meFragment);
                        }
                        transaction.show(meFragment);
                        break;
                    case R.id.rb_more:
                        if (moreFragment == null){
                            moreFragment=new MoreFragment();
                            transaction.add(R.id.fl_main,moreFragment);
                        }
                        transaction.show(moreFragment);
                        break;
                }
                transaction.commit();

            }
        });
        rg_main.check(R.id.rb_home);

    }

    private void hideFragment() {
        if (homeFragment!=null){
            //隐藏
            transaction.hide(homeFragment);
        }
        if (invesFragment!=null){
            //隐藏
            transaction.hide(invesFragment);
        }
        if (meFragment!=null){
            //隐藏
            transaction.hide(meFragment);
        }
        if (moreFragment!=null){
            //隐藏
            transaction.hide(moreFragment);
        }
    }


}

//顺便加个标题栏

//创建一个标题栏布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:background="#05106d"
    android:padding="5dp">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/left"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="标题"
        android:textSize="28sp"
        android:layout_centerInParent="true"
        android:textColor="@color/title_text"
        android:id="@+id/tv_title"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/my_setting_icon"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

//通过include调用

<LinearLayout 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"
    android:orientation="vertical"
    tools:context="com.example.somet.p2pinvest.fragment.InvesFragment">

    <include layout="@layout/common_title"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="投资"
        android:textSize="40sp"
        android:textColor="@color/title_text"/>

</LinearLayout>

//每个页面都可以使用,如果不需要左右图片,可以在java代码中隐藏

imageView对象.setVisibility(View.GONE);

//OK

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页