昨天介绍了BottomNavigationView的使用方法,今天我要分享使用BottomNavigationView切换不同的页面。
首先设定activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@+id/bottomNavigation"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#A9FFFFFF"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:menu="@menu/menu"/>
</androidx.constraintlayout.widget.ConstraintLayout>
新增Fragment1.Activity,Fragment2.Activity,Fragment3.Activity,并各别设计UI
fragment_1.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"
android:background="#FFB700"
tools:context=".Fragment1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="This is Fragment1"
android:textSize="40sp"
android:textColor="#2196F3"/>
</FrameLayout>
fragment_2.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"
android:background="#3044F6"
tools:context=".Fragment2">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="500dp"
android:layout_marginLeft="30dp"
android:text="This is Fragment2"
android:textSize="40sp"
android:textColor="#21F34E" />
</FrameLayout>
fragment_3.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"
android:background="#CDB6D3"
tools:context=".Fragment3">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
android:layout_marginLeft="80dp"
android:text="This is Fragment3"
android:textSize="40sp"
android:textColor="#F35C21" />
</FrameLayout>
MainActivity程序设计
package com.example.newtest;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.MenuItem;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
//宣告FragmentManager跟FragmentTransaction
private FragmentManager fragmentManager;
private FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//显示第一个进来的页面
bottomnavugationset();
BottomNavigationView bottomNavigation=(BottomNavigationView)findViewById(R.id.bottomNavigation);
//监听BottomNavigationView事件
bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//fragmentManager跟fragmentTransaction切换画面
fragmentManager=getSupportFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
switch (item.getItemId()){
case R.id.setting:
fragmentTransaction.replace(R.id.frame,new Fragment1());
fragmentTransaction.commit();
return true;
case R.id.comment:
fragmentTransaction.replace(R.id.frame,new Fragment2());
fragmentTransaction.commit();
return true;
case R.id.account:
fragmentTransaction.replace(R.id.frame,new Fragment3());
fragmentTransaction.commit();
return true;
}
return false;
}
});
}
private void bottomnavugationset() {
fragmentManager=getSupportFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.frame,new Fragment1());
fragmentTransaction.commit();
}
}
成果图
<<: 强型闯入DenoLand[26] - 使用 Deno 打造多线程应用(3)
前言 昨天体验过搭配密码字典的工具来暴力破解,直觉能想到简易的破解的方法,就是针对密码字典里面的密码...
Ev3 是 LEGO® MINDSTORMS® 乐高公司发展的可程序机器人,有原生的专用语法,且可使...
复习一下国中数学应该都有学过函数映射的概念,就是根据特定的规则将输入转换成输出 在做资料处理的过程中...
Virtual Judge ZeroJudge 题意 输入一正整数 N,判断是否为 9 的倍数及输...
Q: 不会设计怎麽办? A: 小编也不会设计,但可以把别人的设计变成网页! 本篇开始将使用tran...