Android开发FrameLayout底部导航栏的实现

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.最终成果页面效果图

 可以的话,点点关注打赏打赏!谢谢

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
实现 Android 底部导航栏可以使用以下步骤: 1. 在布局文件中添加 BottomNavigationView 控件。 2. 在 Java 文件中获取 BottomNavigationView 的实例,并设置导航栏的选项。 3. 创建多个 Fragment 来作为导航栏的选项内容。 4. 在 Java 文件中设置导航栏选项的监听器,根据选项的不同切换 Fragment。 以下是一个基本的 Android 底部导航栏实现示例代码: XML 布局文件: ``` <RelativeLayout <!-- other views --> <FrameLayout android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottom_navigation_view" /> <android.support.design.widget.BottomNavigationView android:id="@+id/bottom_navigation_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" app:menu="@menu/bottom_navigation_menu" /> </RelativeLayout> ``` Java 文件: ``` public class MainActivity extends AppCompatActivity { private BottomNavigationView bottomNavigationView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bottomNavigationView = findViewById(R.id.bottom_navigation_view); bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { switch (menuItem.getItemId()) { case R.id.navigation_home: switchFragment(new HomeFragment()); return true; case R.id.navigation_search: switchFragment(new SearchFragment()); return true; case R.id.navigation_notifications: switchFragment(new NotificationFragment()); return true; } return false; } }); // 默认显示 HomeFragment switchFragment(new HomeFragment()); } private void switchFragment(Fragment fragment) { getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit(); } } ``` 其中,我们使用了 FrameLayout 来作为 Fragment 的容器,将 Fragment 切换时使用了 FragmentTransaction。同时,我们使用了 BottomNavigationView 的 setOnNavigationItemSelectedListener 来监听导航栏选项点击事件,并根据不同选项切换不同的 Fragment。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小舒卿雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值