底部导航栏是 Android 应用中常见的一种导航方式,通常用于切换不同的页面或功能模块。在这里,我们将通过 Fragment 实现底部导航栏的方式进行一个实例精讲。我们将使用 Fragment、FragmentTransaction 以及 BottomNavigationView 等组件。

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

在 res/layout/activity_main.xml 中定义主界面的布局,包含 FrameLayout 用于加载不同的 Fragment,以及 BottomNavigationView 用于底部导航。
<!-- 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">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_navigation" />

    <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. 创建菜单文件:

在 res/menu 文件夹中创建一个菜单文件,用于定义底部导航栏的项。
<!-- res/menu/bottom_nav_menu.xml -->

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/nav_sample1"
        android:title="Sample 1" />
    <item
        android:id="@+id/nav_sample2"
        android:title="Sample 2" />
</menu>

4. 在 MainActivity 中使用 Fragment:

在 MainActivity.java 中使用 Fragment 和 BottomNavigationView。
public class MainActivity extends AppCompatActivity {

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

        // 默认加载第一个 Fragment
        loadFragment(new SampleFragment1());

        // 设置 BottomNavigationView 的监听器
        BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            Fragment fragment;
            switch (item.getItemId()) {
                case R.id.nav_sample1:
                    fragment = new SampleFragment1();
                    break;
                case R.id.nav_sample2:
                    fragment = new SampleFragment2();
                    break;
                default:
                    fragment = new SampleFragment1();
            }
            return loadFragment(fragment);
        });
    }

    // 加载指定的 Fragment
    private boolean loadFragment(Fragment fragment) {
        if (fragment != null) {
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
            return true;
        }
        return false;
    }
}

5. 运行效果:

通过以上步骤,你已经成功实现了一个简单的底部导航栏,用户可以点击导航栏中的项来切换不同的 Fragment,实现了简单的页面导航效果。这是一种常见的 App 导航方式,用户可以方便地切换不同的功能模块。


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