1. 设置底部导航栏4个按钮 上面就是framelayou控件
<?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"
android:orientation="vertical">
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/l1"/>
<LinearLayout
android:id="@+id/l1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/kong"
android:textColor="#FFFF0000"
android:text="首页"/>
<Button
android:id="@+id/service"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/kong"
android:text="全部服务"/>
<Button
android:id="@+id/word"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/kong"
android:text="世界"/>
<Button
android:id="@+id/user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/kong"
android:text="个人中心"/>
</LinearLayout>
</RelativeLayout>
2.创建4个fragment碎片页面
homefragment 、servicefragment 、userfragment 、wordfragment页面 后就可以开始写java文件
package com.example.main1;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button home,word,service,user;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//TODO 默认显示首页页面
HomeFragment home=new HomeFragment();
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.frame,home);
transaction.commit();
//TODO 调取按钮
StartOn();
}
//TODO 找到发现按钮
private void StartOn(){
home=findViewById(R.id.home);
word=findViewById(R.id.word);
service=findViewById(R.id.service);
user=findViewById(R.id.user);
home.setOnClickListener(this);
word.setOnClickListener(this);
service.setOnClickListener(this);
user.setOnClickListener(this);
}
@Override
public void onClick(View v) {
ColorBack();
switch (v.getId()){
case R.id.home:
RePlaceFrament(new HomeFragment());
home.setTextColor(Color.RED);
break;
case R.id.word:
RePlaceFrament(new WordFragment());
word.setTextColor(Color.RED);
break;
case R.id.service:
RePlaceFrament(new ServiceFragment());
service.setTextColor(Color.RED);
break;
case R.id.user:
RePlaceFrament(new UserFragment());
user.setTextColor(Color.RED);
break;
}
}
private void RePlaceFrament(Fragment fragment){ //TODO 用于替换Fragment。它接收一个Fragment作为参数,表示要替换成的新Fragment
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.frame,fragment);
transaction.commit();
}
private void ColorBack(){
//TODO 让每次点击后都返回 黑色字体
home.setTextColor(Color.BLACK);
word.setTextColor(Color.BLACK);
service.setTextColor(Color.BLACK);
user.setTextColor(Color.BLACK);
}
}
3.最终成果页面效果图
可以的话,点点关注打赏打赏!谢谢