1、在build.gradle 文件中添加
implementation "androidx.navigation:navigation-fragment:2.1.0-alpha02"
implementation "androidx.navigation:navigation-ui:2.1.0-alpha02"
implementation 'com.google.android.material:material:1.1.0-alpha05'
2、在res下的menu目录下新建一个navigation.xml文件 ,用来显示底部Tab标签
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_home_flag"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/nav_dashboard_flag"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_dashboard" />
<item
android:id="@+id/nav_notification_flag"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_notifications" />
</menu>
3、创建一个展示UI界面的activity_bottom_navigatin.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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.BottomNavigationActivity">
<fragment
android:id="@+id/frag_nav_simple"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/navigation"
app:navGraph="@navigation/navation_dev"
app:defaultNavHost="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
android:layout_alignParentBottom="true"
app:menu="@menu/navigation" />
</RelativeLayout>
4、在res 下新建一级navigation 目录中新建一个navigation_dev.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"
android:id="@+id/navation_dev"
app:startDestination="@id/nav_home_flag">
<fragment
android:id="@+id/nav_home_flag"
android:name="com.example.jetpackdemo.ui.HomeFragment"
android:label="home flag">
<action
android:id="@+id/action_nav_home_flag_to_nav_dashboard_flag"
app:destination="@id/nav_dashboard_flag" />
</fragment>
<fragment
android:id="@+id/nav_dashboard_flag"
android:name="com.example.jetpackdemo.ui.DashboardFragment"
android:label="dashboard flag">
</fragment>
<fragment
android:id="@+id/nav_notification_flag"
android:name="com.example.jetpackdemo.ui.NotificationFragment"
android:label="notification flag">
<action
android:id="@+id/action_nav_notification_flag_to_nav_home_flag"
app:destination="@id/nav_home_flag" />
<action
android:id="@+id/action_nav_notification_flag_to_placeholder2"
app:destination="@id/placeholder" />
<deepLink
android:id="@+id/deepLink"
app:uri="http://www.baidu.com" />
</fragment>
<fragment android:id="@+id/placeholder" />
</navigation>
5、在Activity 中调用:
public class BottomNavigationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_navigation);
BottomNavigationView navigation = findViewById(R.id.navigation);
FragmentManager fragmentManager = getSupportFragmentManager();
NavHostFragment navHostFragment = (NavHostFragment)fragmentManager.findFragmentById(R.id.frag_nav_simple);
NavController navController = navHostFragment.getNavController();
NavigationUI.setupWithNavController(navigation,navController);
}
}
6、效果图: