在这里,我们将通过 Fragment 和 FragmentContainerView 以及 Navigation Component 来实现底部导航栏。使用 Navigation Component 可以更好地管理和导航 Fragment,提高代码的模块化和可维护性。

1. 创建项目和布局文件:

在 res/layout/activity_main.xml 中定义主界面的布局,包含 FragmentContainerView 用于容纳不同的 Fragment。
<!-- res/layout/activity_main.xml -->

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_above="@+id/bottom_navigation"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_nav_menu" />
</RelativeLayout>

2. 创建 Fragment:

创建两个或更多的 Fragment,用于展示不同的页面内容。
// SampleFragment1.java
public class SampleFragment1 extends Fragment {
    // Fragment 1 的布局文件,内容可以根据实际需求进行修改
    // ...

    public SampleFragment1() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_sample1, container, false);
    }
}

// SampleFragment2.java
public class SampleFragment2 extends Fragment {
    // Fragment 2 的布局文件,内容可以根据实际需求进行修改
    // ...

    public SampleFragment2() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_sample2, container, false);
    }
}

3. 创建 Navigation Graph:

在 res/navigation 文件夹中创建一个 Navigation Graph 文件,用于定义底部导航栏的项和 Fragment 之间的导航关系。
<!-- res/navigation/nav_graph.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/nav_graph"
    app:startDestination="@id/sampleFragment1">

    <fragment
        android:id="@+id/sampleFragment1"
        android:name="com.example.app.SampleFragment1"
        android:label="Sample 1"
        tools:layout="@layout/fragment_sample1" />

    <fragment
        android:id="@+id/sampleFragment2"
        android:name="com.example.app.SampleFragment2"
        android:label="Sample 2"
        tools:layout="@layout/fragment_sample2" />
</navigation>

4. 在 MainActivity 中使用 Navigation Component:

在 MainActivity.java 中使用 Navigation Component。
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 设置 BottomNavigationView 的监听器
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupWithNavController(bottomNavigationView, navController);
    }
}

5. 运行效果:

通过以上步骤,你已经成功实现了一个底部导航栏,并且可以通过点击底部导航项来切换不同的 Fragment 页面。使用 Navigation Component 管理导航关系使代码更加清晰和模块化。


转载请注明出处:http://www.pingtaimeng.com/article/detail/15184/Android