Write catalog title here
Introduction
BottomNavigationView is the bottom navigation component under Google's material. Generally speaking, it is convenient and easy to use
Blog如有不对,敬请斧正
喜欢Android的可以关注我,日常更新Android干货
看都看到这了,加个关注叭!
建议收藏,防止找不到,哈哈
Import
implementation 'com.google.android.material:material:1.2.1'
(1.2.1 is currently the latest version)
use
1. Create the main page 加入BottomNavigationView
activity_nav_bottom.xml:
<?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:layout_height="match_parent"
android:orientation="vertical"
>
<FrameLayout
android:id="@+id/home_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginBottom="2dp"
android:layout_weight="1"
android:background="#ffffff" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_bottom"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#ffffff"
app:menu="@menu/menu_nav"
/>
</LinearLayout>
2. 创建导航用的菜单项
Set the id, title, and icon attributes for BottomNavigationView
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_one"
android:title="一"
android:icon="@drawable/ic_find"
/>
<item
android:id="@+id/menu_two"
android:title="二"
android:icon="@drawable/ic_video"
/>
<item
android:id="@+id/menu_three"
android:title="三"
android:icon="@drawable/ic_my"
/>
<item
android:id="@+id/menu_four"
android:title="四"
android:icon="@drawable/ic_accunt"
/>
</menu>
3. 创建Fragment及其布局
I created a total of 4 layouts for the menu items in BottomNavigationView , a simple demonstration:
package com.example.myapplication.nav_bottom1;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.myapplication.R;
public class fragment_one extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_one,container,false);
}
}
<?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:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="ONE"
/>
</LinearLayout>
4. Implement BottomNavigationView.OnNavigationItemSelectedListener for the menu items in 设置监听
BottomNavigationView (1)
(2), useFragmentTransaction切换不同的Fragment
package com.example.myapplication.nav_bottom1;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.MenuItem;
import com.example.myapplication.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class nav_bottom extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
private BottomNavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav_bottom);
navigationView = findViewById(R.id.nav_bottom);
navigationView.setOnNavigationItemSelectedListener(this);
navigationView.setSelectedItemId(R.id.menu_one);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
switch (item.getItemId()){
case R.id.menu_one:
fragmentTransaction.replace(R.id.home_fragment, new fragment_one()).commit();
return true;
case R.id.menu_two:
fragmentTransaction.replace(R.id.home_fragment,new fragment_two()).commit();
return true;
case R.id.menu_three:
fragmentTransaction.replace(R.id.home_fragment,new fragment_three()).commit();
return true;
case R.id.menu_four:
fragmentTransaction.replace(R.id.home_fragment,new fragment_four()).commit();
return true;
}
return true;
}
}
to sum up
BottomNavigationView is a more popular and officially recommended component. It has many attributes and is very flexible to use.