保姆级手把手记录Android studio BottomNavigationView +FrameLayout暴力切换Fragment

开发环境:

效果图:

《《《代码在底部》》》

1,新建项目

2,新建若干Fragment,内容一样,改一下显示出来的Text,名字分别为test1Fragment,test2Fragment,test3Fragment,默认TextView的Text属性分别设置为Fragment1内容,Fragment2内容,Fragment3内容,以示区分

3,拖拽一个FrameLayout

4,拖拽一个BottomNavigationView

5,调整代码

6,新建一个菜单资源给BottomNavigationView使用

7,BottomNavigationView和新建的菜单项关联

8,进一步调整代码

9,MainActivity中的代码

10,红色部分导入类即可解决

代码如下:

1,nav_menu.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/navigation_home"
        android:icon="@android:drawable/ic_dialog_map"
        android:title="Frame1" />
    <item
        android:id="@+id/navigation_tools"
        android:icon="@android:drawable/ic_menu_compass"
        android:title="Frame2" />
    <item
        android:id="@+id/navigation_play"
        android:icon="@android:drawable/ic_menu_gallery"
        android:title="Frame3" />
</menu>

2,activity_main.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="ExtraText">


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

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/nav_menu"
        tools:layout_height="50dp" />


</RelativeLayout>

3,fragment_test1.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".test1Fragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/text1Fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Fragment1内容" />

</FrameLayout>

4,MainActivity.java

复制代码
package com.example.bottomnavigationviewstudy;

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

import android.os.Bundle;
import android.view.MenuItem;

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

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
    test1Fragment t1f = new test1Fragment();
    test2Fragment t2f = new test2Fragment();
    test3Fragment t3f = new test3Fragment();

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

        BottomNavigationView navView = findViewById(R.id.nav_menu);
        mFragments.add(t1f);
        mFragments.add(t2f);
        mFragments.add(t3f);

        //设置默认加载视图
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new test1Fragment()).commit();

        // navView 点击事件
        navView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                String a = (String) item.getTitle();

                //判断是哪个item被点击了
                switch (a) {

                    case "Frame2":
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new test2Fragment()).commit();
                        break;
                    case "Frame3":
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new test3Fragment()).commit();
                        break;
                    default:
                        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new test1Fragment()).commit();
                        break;
                }
                item.setChecked(true);
                return true;
            }
        });
    }
}
相关推荐
weixin_55008315几秒前
QTdesigner配置在pycharm里使用anaconda环境配置安装成功
ide·python·pycharm
有位神秘人15 分钟前
Android中获取当前屏幕的宽高工具类
android
dvlinker31 分钟前
C/C++编程开发工具及实用软件推荐
ide·vscode·visual studio·qt creator·c/c++·source insight·编程工具
Yang-Never34 分钟前
Open GL ES -> 应用前后台、Recent切换,SurfaceView纹理贴图闪烁问题分析解决
android·开发语言·kotlin·android studio·贴图
liujun35121591 小时前
camera开发,我对预览请求的理解
android
无法长大1 小时前
Mac M1 环境下使用 Rust Tauri 将 Vue3 项目打包成 APK 完整指南
android·前端·macos·rust·vue3·tauri·打包apk
一只程序熊1 小时前
uniappx 配置 uni.chooseLocation 地图
android·uni-app x
Kazefuku1 小时前
VS Code 和Visual Studio:简单易懂的区别
ide·windows·visual studio
Yang-Never1 小时前
Android 应用启动 -> Android 多种方式启动同一进程,Application.onCreate() 会多次执行吗?
android·java·开发语言·kotlin·android studio
2501_916008891 小时前
iOS 开发助手工具,设备信息查看、运行日志、文件管理等方面
android·ios·小程序·https·uni-app·iphone·webview