昨天介紹了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();
}
}
成果圖