效果图
- 这里提一下
BottomNavigationView 布局动画 可以通过代码取消,比较简单的是直接在布局文件中加一句:
app:labelVisibilityMode="labeled"
效果图如下
用法
- xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fl_content"
android:layout_above="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_alignParentBottom="true"
android:id="@+id/navigation"
app:labelVisibilityMode="labeled"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@color/grey_btn_color_disable"
app:elevation="1dp"
app:menu="@menu/navigation_item"/>
</RelativeLayout>
- menu文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/nav_measuring"
android:icon="@mipmap/measuring"
android:title="开始" />
<item android:id="@+id/nav_Dailyreport"
android:icon="@mipmap/report"
android:title="报告" />
<item android:id="@+id/nav_shop"
android:icon="@mipmap/shop"
android:title="商城" />
<item android:id="@+id/nav_friend"
android:icon="@mipmap/friend"
android:title="亲友" />
<item android:id="@+id/nav_mine"
android:icon="@mipmap/mine"
android:title="个人"/>
</menu>
- 接下来就是创建activity_measuringframent.xml
- 剩下的3个就不列举了
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvmeasuring_text"
android:textColor="@color/black"
android:textSize="25sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="measuring"
/>
</LinearLayout>
- 创建MeasuringFragment 继承Fragment 重写里面的onCreateView方法
- 剩下3fragment个就不列举了
public class MeasuringFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.activity_measuringframent,container,false);
return view;
}
}
- 接下来 MainActivity
public class MainActivity extends BaseActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
private FrameLayout flContent;
private BottomNavigationView navigation;
private int preFragment = 0;// 记录上一个被点击的 fragment页面 ,默认值是0
private List<Fragment> fragments;
@Override
protected void initData() {
initview();
fragments=new ArrayList<>();
fragments.add(new MeasuringFragment());
fragments.add(new ReportFragment());
fragments.add(new ShopFragment());
fragments.add(new FriendFragment());
fragments.add(new MineFragment());
//默认显示第一个fragment
getSupportFragmentManager().beginTransaction().replace(R.id.fl_content,fragments.get(0)).commit();
navigation.setOnNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_measuring:
if(preFragment!=0){
switchFragemnt(preFragment,0);
preFragment=0;
}
return true;
case R.id.nav_Dailyreport:
if(preFragment!=1){
switchFragemnt(preFragment,1);
preFragment=1;
}
return true;
case R.id.nav_shop:
if(preFragment!=2){
switchFragemnt(preFragment,2);
preFragment=2;
}
return true;
case R.id.nav_friend:
if(preFragment!=3){
switchFragemnt(preFragment,3);
preFragment=3;
}
return true;
case R.id.nav_mine:
if(preFragment!=4){
switchFragemnt(preFragment,4);
preFragment=4;
}
return true;
}
return false;
}
private void switchFragemnt(int preFragment, int i) {
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.hide(fragments.get(preFragment));
//如果当前被点击的fragment页面还没有被加入到 FragmentManager 里面,则需要添加进来
if(fragments.get(i).isAdded()==false){
ft.add(R.id.fl_content,fragments.get(i));
}
ft.show(fragments.get(i)).commitAllowingStateLoss();
}
@Override
protected int initLayoutResource() {
return R.layout.activity_main;
}
//实例化
private void initview() {
flContent=findViewById(R.id.fl_content);
navigation= findViewById(R.id.navigation);
}
}
以上是整个流程
希望能帮到你
连接地址:ButtomNavigation