android studio底部导航栏

实现底部导航栏切换

将java文件return的xml文件赋值给页面FrameLayout控件
java文件BottomNavigationView,监听器setOnNavigationItemSelectedListener
MainActivity.java代码
复制代码
package com.example.myapplication;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;


import com.google.android.material.bottomnavigation.BottomNavigationView;

public class MainActivity extends AppCompatActivity {
    private BottomNavigationView bottomNavigationView;

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

        bottomNavigationView = findViewById(R.id.bottom_navigation);

        // 默认显示首页
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.frame_layout, new HomeFragment())
                .commit();

        // 设置切换监听
        bottomNavigationView.setOnNavigationItemSelectedListener(item -> {
            Fragment selectedFragment;
            switch (item.getItemId()) {
                case R.id.nav_search:
                    selectedFragment = new SearchFragment();
                    break;
                case R.id.nav_profile:
                    selectedFragment = new ProfileFragment();
                    break;
                case R.id.nav_home:
                default:
                    selectedFragment = new HomeFragment();
                    break;
            }

            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.frame_layout, selectedFragment)
                    .commit();
            return true;
        });
    }
}
复制代码
HomeFragment.java代码:
复制代码
package com.example.myapplication;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.zip.Inflater;

public class HomeFragment extends Fragment {
    private TextView homeText;
    private Button updateButton;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        // 加载布局文件
        View view = inflater.inflate(R.layout.fragment_home, container, false);

        // 获取控件
        homeText = view.findViewById(R.id.home_text);
        updateButton = view.findViewById(R.id.update_button);

        // 设置按钮点击事件
        updateButton.setOnClickListener(v -> {
            homeText.setText("文字已更新!");
        });

        return view;
    }
}
复制代码
ProfileFragment.java代码:
复制代码
package com.example.myapplication;

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;

public class ProfileFragment extends Fragment {
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }
}
复制代码
SearchFragment.java代码:
复制代码
package com.example.myapplication;

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;

public class SearchFragment extends Fragment {
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_search, container, false);
    }
}
复制代码
activity_main4.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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 用于切换内容的 Fragment 容器 -->
    <FrameLayout
        android:id="@+id/frame_layout"
        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"
        app:menu="@menu/bottom_nav_menu"/>
</RelativeLayout>
复制代码
fragment_home.xml代码:
复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="16dp">

<TextView
    android:id="@+id/home_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="这是首页"
    android:textSize="24sp"
    android:padding="16dp" />

<Button
    android:id="@+id/update_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="点我更新文字" />
</LinearLayout>
复制代码
fragment_search.xml代码:
复制代码
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="HOME 1"
    android:textSize="30sp" />

在resources/menu/bottom_nav_menu.xml代码如下:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/nav_home"
        android:title="首页"
        android:icon="@android:drawable/ic_menu_view" />
    <item
        android:id="@+id/nav_search"
        android:title="搜索"
        android:icon="@android:drawable/ic_menu_search" />
    <item
        android:id="@+id/nav_profile"
        android:title="我的"
        android:icon="@android:drawable/ic_menu_myplaces" />
</menu>

运行结果:

相关推荐
恋猫de小郭2 小时前
Meta 宣布加入 Kotlin 基金会,将为 Kotlin 和 Android 生态提供全新支持
android·开发语言·ios·kotlin
summer夏1232 小时前
2025.07 做什么
java·android studio
aqi002 小时前
FFmpeg开发笔记(七十七)Android的开源音视频剪辑框架RxFFmpeg
android·ffmpeg·音视频·流媒体
androidwork4 小时前
深入解析内存抖动:定位与修复实战(Kotlin版)
android·kotlin
梦天20155 小时前
android核心技术摘要
android
szhangbiao6 小时前
“开发板”类APP如果做屏幕适配
android
笑醉踏歌行7 小时前
idea应用代码配色网站
java·ide·intellij-idea
高林雨露7 小时前
RecyclerView中跳转到最后一条item并确保它在可视区域内显示
android
韩初心9 小时前
使用 visual studio 2022 编译 Lua5.4.8
ide·visual studio·lua5.4
移动开发者1号10 小时前
ReLinker优化So库加载指南
android·kotlin