一,前言:最近看了下JetPack架构,做下笔记。BottomNavigationView+Navigation实现底部菜单。
准备工作:
新建底部menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/homeFragment"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/notificationsFragment"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_dashboard" />
<item
android:id="@+id/dashboardFragment"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_notifications" />
</menu>
新建Fragment
public class HomeFragment extends Fragment {
FragmentHomeBinding binding;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
View view = binding.getRoot();
return view;
}
}
xml
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data></data>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home页面"
/>
</LinearLayout>
</layout>
navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/navigation"
app:startDestination="@id/homeFragment"
tools:ignore="UnusedNavigation">
<fragment
android:id="@+id/homeFragment"
android:name="com.cwj.navigation.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home">
</fragment>
<fragment
android:id="@+id/notificationsFragment"
android:name="com.cwj.navigation.NotificationsFragment"
android:label="fragment_notifications"
tools:layout="@layout/fragment_notifications">
</fragment>
<fragment
android:id="@+id/dashboardFragment"
android:name="com.cwj.navigation.DashboardFragment"
android:label="dashboard"
tools:layout="@layout/dashboard">
</fragment>
</navigation>
MainActivity
package com.cwj.navigation;
import android.os.Bundle;
import com.cwj.navigation.databinding.ActivityMainBinding;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentManager;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.fragment.NavHostFragment;
import androidx.navigation.ui.NavigationUI;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding mainBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mainBinding = DataBindingUtil. setContentView(this, R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
NavHostFragment navHostFragment = (NavHostFragment) fragmentManager.findFragmentById(R.id.fragments);
NavController navController = navHostFragment.getNavController();
NavigationUI.setupWithNavController(mainBinding.navView, navController);
}
@Override
public boolean onSupportNavigateUp() {
return Navigation.findNavController(this,R.id.nav_view).navigateUp();
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragments"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
结尾:这就写完了。代码量少了很多。简洁