AndroidStudio制作底部导航栏以及用Fragment实现切换功能

前言

大家好,我是 Vic,今天给大家带来AndroidStudio制作底部导航栏以及用Fragment实现切换功能的概述,希望你们喜欢

学习目标

AndroidStudio制作底部导航栏以及用Fragment实现切换功能,用户点击底部导航栏可以实现三个模块的跳转。

图片资源

需要底部导航栏三个点击按钮的图片资源
main_button_1.png,main_button_2.png,main_button_3.png

以及点击变换的图片资源
main_button_1_selected.png,main_button_2_selected.png,main_button_3_selected.png

以上图片资源都放进drawable文件夹中

activity_main 布局

在 MainActivity 页面中主要有两个区域:

  • 一个是放 Fragment 的 main_body
  • 一个是放底部导航栏的 main_bottom_bar

主要的Fragment代码块:

<LinearLayout
        android:orientation="vertical"
        android:background="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--标题栏-->
        <include layout="@layout/main_title_bar" />
        <!--放置Fragment的main_body-->
        <RelativeLayout
            android:id="@+id/main_body"
            android:background="@android:color/white"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </RelativeLayout>
</LinearLayout>

主要的底部导航栏的代码块:

<!--实现在底部,水平排列按钮-->
<LinearLayout
     android:id="@+id/main_bottom_bar"
     android:layout_alignParentBottom="true"
     android:background="#F2F2F2"
     android:orientation="horizontal"
     android:layout_width="match_parent"
     android:layout_height="55dp">
    <RelativeLayout
         android:layout_weight="1"
         android:id="@+id/bottom_bar_1_btn"
         android:layout_width="0dp"
         android:layout_height="match_parent">
         <TextView
             android:id="@+id/bottom_bar_text_1"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:layout_alignParentBottom="true"
             android:layout_centerHorizontal="true"
             android:layout_marginBottom="3dp"
             android:gravity="center"
             android:singleLine="true"
             android:text="button_1"
             android:textColor="#666666"
             android:textSize="14sp"/>
         <ImageView
             android:layout_width="27dp"
             android:layout_height="27dp"
             android:layout_above="@+id/bottom_bar_text_1"
             android:layout_alignParentTop="true"
             android:layout_centerHorizontal="true"
             android:layout_marginTop="3dp"
             android:id="@+id/bottom_bar_image_1"
             android:src="@drawable/main_button_1"/>
     </RelativeLayout>
    ....<!--布局的代码总是繁琐又无聊的,记得要自己补全-->
</LinearLayout>

实例化控件

实例化控件一些琐碎的代码:

//先实例化控件,那我给出自己打的实例化代码
//来自main_title_bar.xml
    private TextView tv_main_title;//标题
    private TextView tv_back;//返回按钮
    private RelativeLayout title_bar;
//来自activity_main.xml
    private RelativeLayout main_body;
    private TextView bottom_bar_text_1;
    private ImageView bottom_bar_image_1;
    ...
    private LinearLayout main_bottom_bar;
    private RelativeLayout bottom_bar_1_btn;
    private RelativeLayout ...;

然后

initView();
//实例化
private void initView(){
        //标题显示
        tv_back=findViewById(R.id.tv_back);
        tv_main_title=findViewById(R.id.tv_main_title);
        title_bar=findViewById(R.id.title_bar);
        //底部导航栏
        main_body=findViewById(R.id.main_body);
        bottom_bar_text_1=findViewById(R.id.bottom_bar_text_1);
        bottom_bar_image_1=findViewById(R.id.bottom_bar_image_1);
        ...
        //包含底部 android:id="@+id/main_bottom_bar"
        main_bottom_bar=findViewById(R.id.main_bottom_bar);
        //private RelativeLayout bottom_bar_1_btn;
        bottom_bar_1_btn=findViewById(R.id.bottom_bar_1_btn);
        //添加点击事件
       bottom_bar_1_btn.setOnClickListener(this);
       ...
       //技巧
       //tv_back.setVisibility(View.GONE);
        tv_main_title.setText("课程");
        title_bar.setBackgroundColor(Color.parseColor("#30B4FF"));
}

底部导航栏状态的切换方法

给MainActivity加一个setSelectStatus() 方法,方法里用参数index来判断当前选的按钮

示例代码

private void setSelectStatus(int index) {
        switch (index){
            case 0:
              //图片点击选择变换图片,颜色的改变,其他变为原来的颜色,并保持原有的图片
              bottom_bar_image_1.setImageResource(R.drawable.main_button_1_selected);
              bottom_bar_text_1.setTextColor(Color.parseColor("#0097F7"));
              //其他的文本颜色不变
              bottom_bar_text_2.setTextColor(Color.parseColor("#666666"));
              bottom_bar_text_3.setTextColor(Color.parseColor("#666666"));
              //图片也不变
              bottom_bar_image_2.setImageResource(R.drawable.main_button_2);
              bottom_bar_image_3.setImageResource(R.drawable.main_button_3);
             break;
            case 1://同理如上
             ...
             break;
            case 2://同理如上
             ...
             break;
  }
}

实现底部导航栏的响应

导航栏文本颜色和图片切换效果的方法写好了,接下来是点击响应的方法

给MainActivity加上View.OnClickListener接口

在生成的 onClick() 方法中加上导航栏区域的响应

@Override
    public void onClick(View v) {
        switch (v.getId()){
          case R.id.bottom_bar_1_btn:
                setSelectStatus(0);
                break;
          case R.id.bottom_bar_2_btn:
                setSelectStatus(1);
                break;
          case R.id.bottom_bar_3_btn:
                setSelectStatus(2);
                break;
        }
}

别忘了在initView() 中添加监听器

bottom_bar_1_btn.setOnClickListener(this);

三个 fragment 的创建

就是简单的创建三个布局,展现Fragment_1/2/3 即可

示例代码块

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white">
    <TextView
        android:text="Fragment_1"
        android:textColor="@android:color/black"
        android:textSize="50sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

然后通过我之前写的插件自动生成三个Fragemnt ,就可以了不用管生成的Fragement_1/2/3.java文件了,
插件文章
《 Android开发的插件Code Generator与LayoutCreator的安装与使用,提升你的开发效率 》

三个fragment的显示和切换

在MainActivity里把AppCompatActivity改为FragmentActivity

把Fragment加到Activity里的代码
通常用这个来展示,但是代码过长,我们来简化一下

/*
* FragmentManager manager = getSupportFragmentManager();
* FragmentTransaction transaction = manager.beginTransaction();
* transaction.add(R.id.main_body,new CourseFragment()).commit();
* */

我们先来添加一个setMain() 方法,来显示打开界面时,显示的初始页面

/用于打开初始页面
 private void setMain() {
      //getSupportFragmentManager() -> beginTransaction() -> add -> (R.id.main_boy,显示课程 new CourseFragment()
      this.getSupportFragmentManager().beginTransaction().add(R.id.main_body,new CourseFragment()).commit();
 }

上面的代码中可以看到相对来说比较少,那我们就用这个,然后我们来实现点击底部导航栏来切换响应的fragment,我们在onClick()中添加即可。

case R.id.bottom_bar_1_btn:
                //添加
                getSupportFragmentManager().beginTransaction().replace(R.id.main_body,new Button_1Fragment()).commit();
                setSelectStatus(0);
                break;

如果觉得不错,那就点个赞吧!❤️

总结

  • 本文讲了AndroidStudio制作底部导航栏以及用Fragment实现切换功能,界面的布局介绍,如果您还有更好地理解,欢迎沟通
  • 定位:分享 Android&Java知识点,有兴趣可以继续关注
  • 17
    点赞
  • 101
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
### 回答1: Android Studio中的Fragment底部导航栏是一种常见的UI设计模式,它可以让用户快速切换不同的页面或功能。在实现底部导航栏时,可以使用Android官方提供的BottomNavigationView控件,该控件可以方便地创建底部导航栏,并且支持多种样式和自定义选项。在使用BottomNavigationView时,需要创建多个Fragment,并在底部导航栏中添加对应的菜单项,然后在点击菜单项时切换到对应的Fragment。此外,还可以使用ViewPager和FragmentPagerAdapter来实现底部导航栏的滑动切换效果。总之,Android Studio提供了丰富的工具和组件来帮助开发者实现各种UI设计,包括底部导航栏。 ### 回答2: Android Studio中的Fragment底部导航栏是一种常用的界面控件,它为用户提供了方便的导航和操作方式。Fragment底部导航栏通常放置在屏幕最底部,可以显示多个图标,每个图标表示一个不同的功能或页面。当用户点击某个图标时,应用程序将自动显示相应的Fragment实现了快速切换不同页面的功能。 开发者可以通过使用Android Studio提供的各种控件和API,来创建并配置Fragment底部导航栏。其中最常见的是使用BottomNavigationView控件,它可以在布局文件中直接添加,然后在Java代码中设置监听器以响应用户点击事件,将不同的Fragment进行切换。 除了使用BottomNavigationView外,还可以通过自定义控件,实现更丰富、更灵活的底部导航栏。例如,可以使用Material Design组件,来创建具有动画效果、菜单弹出式选项、图标和文字等自定义特性的导航栏。此外,为了确保底部导航栏的完整性,可以考虑使用CoordinatorLayout和AppBarLayout组件,以便在不同情况下自动隐藏或显示导航栏。 在使用Android Studio Fragment底部导航栏时,需要注意一些细节。例如,在替换Fragment时,应该使用add和remove方法,或者使用replace方法,以确保UI界面的平稳过渡。另外,为避免低效率的页面切换导航栏响应,建议使用AsyncTask或其他线程技术,以提高应用的性能和流畅度。 总之,Android Studio Fragment底部导航栏是一种非常有用的界面控件,能够极大地提升应用程序的用户体验和操作效率。通过深入了解其应用方法和技术要点,可以更好地发挥其潜力,满足用户需求,并提高应用的竞争力。 ### 回答3: Android Studio的fragment底部导航栏是一个非常常用的功能。它使得应用程序用户可以更加方便地切换应用程序的不同页面。在本文中,我们将介绍如何使用Android Studio创建底部导航栏。 要在Android Studio中创建底部导航栏,您需要先创建一个新项目或打开现有项目。然后,在Project视图中,右键单击app文件夹并选择New->Activity->Bottom Navigation Activity。在弹出的新窗口中,您可以选择要在Android Studio中创建的底部导航栏的各个页面的数量。 创建该项目后,您可以将自己的代码添加到不同的片段中,以便在底部导航栏中轻松导航。此外,您可以自定义底部导航栏来控制其中的按钮和外观,例如将标签更改为图标,将颜色更改为与应用程序配色等。 在这个过程中,您可以使用Android Studio提供的多种功能来增强您的底部导航栏。例如,您可以使用代码自动完成来更快地输入代码,或者使用Android Studio的布局编辑器来更轻松地在布局中添加和删除元素。 在您创建底部导航栏后,您可以使用Android Studio的AVD管理器在模拟器中测试它,并确保它正常工作。一旦您满意结果,您就可以将应用程序打包并发布到Google Play商店或其他应用程序市场中。 总的来说,在Android Studio中创建底部导航栏是非常容易的。通过使用Android Studio提供的功能和工具,您可以创建一个整洁、功能强大的应用程序,这会让您的用户更加喜欢。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值