BottomNavigationView 实现底部导航栏
一、先导入依赖
// BottomNavigationView implementation 'com.android.support:design:27.1.1'
二、(1)、在res -->创建menu 文件夹 --> 在写 navigation 这个xml文件 ,里面是你创建的底部按钮的名称和图片
如图:
(2)、菜单里面的代码
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/jingxuan" android:icon="@drawable/found" android:title="精选" /> <item android:id="@+id/zhuanti" android:icon="@drawable/special" android:title="专题" /> <item android:id="@+id/faxian" android:icon="@drawable/fancy" android:title="发现" /> <item android:id="@+id/wode" android:icon="@drawable/my" android:title="我的" /> </menu>
三、在布局里面引用控件
<?xml version="1.0" encoding="utf-8"?> <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:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:textSize="20dp" android:gravity="center" android:background="#f89" android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="标题"/> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:id="@+id/fram_layout"></FrameLayout> <android.support.design.widget.BottomNavigationView android:id="@+id/bottomBar" app:menu="@menu/navigation" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>
四、在MainActivity里面加载fragment -->主要代码
package com.xwj.lenovo.xuwenjuan0514; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.design.widget.BottomNavigationView; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.view.MenuItem; import android.widget.FrameLayout; import android.widget.TextView; import com.xwj.lenovo.xuwenjuan0514.view.fragment.Fragment_faxian; import com.xwj.lenovo.xuwenjuan0514.view.fragment.Fragment_jingxuan; import com.xwj.lenovo.xuwenjuan0514.view.fragment.Fragment_wode; import com.xwj.lenovo.xuwenjuan0514.view.fragment.Fragment_zhuanti; import butterknife.BindView; import butterknife.ButterKnife; public class MainActivity extends AppCompatActivity { @BindView(R.id.fram_layout) FrameLayout framLayout; @BindView(R.id.bottomBar) BottomNavigationView bottomBar; @BindView(R.id.text) TextView text; private FragmentManager supportFragmentManager; private Fragment_jingxuan fragment_jingxuan; private Fragment_wode fragment_wode; private Fragment_faxian fragment_faxian; private Fragment_zhuanti fragment_zhuanti; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); supportFragmentManager = getSupportFragmentManager(); fragment_jingxuan = new Fragment_jingxuan(); // 默认显示第一个fragment supportFragmentManager.beginTransaction().add(R.id.fram_layout, fragment_jingxuan).commit(); bottomBar.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { private FragmentTransaction fragmentTransaction; @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { // 每次点击时 隐藏所有的fragment hideAllFragments(); // 开启一个事务 fragmentTransaction = supportFragmentManager.beginTransaction(); // 根据点击的按钮 显示对应的fragment switch (item.getItemId()) { case R.id.jingxuan: if (fragment_jingxuan == null) { fragment_jingxuan = new Fragment_jingxuan(); // 如果这个碎片是空的 就把这个碎片添加到帧布局中进行显示 fragmentTransaction.add(R.id.fram_layout, fragment_jingxuan); text.setText("精选"); } else { // 如果这个碎片不是空的 直接显示这个碎片 fragmentTransaction.show(fragment_jingxuan); text.setText("精选"); } fragmentTransaction.commit(); return true; case R.id.zhuanti: if (fragment_zhuanti == null) { fragment_zhuanti = new Fragment_zhuanti(); fragmentTransaction.add(R.id.fram_layout, fragment_zhuanti); text.setText("专题"); }else { // 如果这个碎片不是空的 直接显示这个碎片 fragmentTransaction.show(fragment_zhuanti); text.setText("专题"); } fragmentTransaction.commit(); return true; case R.id.faxian: if (fragment_faxian == null) { fragment_faxian = new Fragment_faxian(); fragmentTransaction.add(R.id.fram_layout, fragment_faxian); text.setText("发现"); }else { // 如果这个碎片不是空的 直接显示这个碎片 fragmentTransaction.show(fragment_faxian); text.setText("发现"); } fragmentTransaction.commit(); return true; case R.id.wode: if (fragment_wode == null) { fragment_wode = new Fragment_wode(); fragmentTransaction.add(R.id.fram_layout, fragment_wode); text.setText("我的"); }else { // 如果这个碎片不是空的 直接显示这个碎片 fragmentTransaction.show(fragment_wode); text.setText("我的"); } fragmentTransaction.commit(); return true; } return false; } }); } // 隐藏所有的fragment public void hideAllFragments() { // 开启一个事务 FragmentTransaction hideTran = supportFragmentManager.beginTransaction(); // 隐藏fragment if (fragment_jingxuan != null) hideTran.hide(fragment_jingxuan); if (fragment_zhuanti != null) hideTran.hide(fragment_zhuanti); if (fragment_faxian != null) hideTran.hide(fragment_faxian); if (fragment_wode != null) hideTran.hide(fragment_wode); // 提交事务 hideTran.commit(); } }
写上你需要的fragment页面,以上基本就完成了。