安卓APP底部导航栏(有消息圆点指示器),实现fragment切换(eclipse)

本文介绍了如何使用相对布局和单选按钮在安卓应用中实现底部导航栏,包括消息圆点指示器。通过设置选择器控制状态切换,使用Fragment进行页面动态添加、删除和替换。同时提供了fragment的切换方法和代码重构的实践建议。
摘要由CSDN通过智能技术生成

本项目使用了相对布局和单选按钮实现了安卓app常见的底部导航栏(带有消息圆点指示器),效果如果所示


一、布局代码如下:

<RelativeLayout 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"
    tools:context=".MainActivity" >


    <!-- Fragment显示内容的容器 -->
    <FrameLayout
        android:id="@+id/fl_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/nav_layout" />
    
    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_above="@+id/nav_layout"
        android:background="@color/line_bg" />


    <LinearLayout
    android:id="@+id/nav_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal">
    <!-- 首页 -->
   <RelativeLayout
       android:id="@+id/rlHomePage"
       android:layout_width="0dp"
       android:layout_height="match_parent"
       android:padding="2dp"
       android:background="@drawable/selector_nav_rl"
       android:layout_weight="2">


       <RadioButton
           android:id="@+id/rbtnHomePage"
           style="@style/nav_radio_style"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_centerInParent="true"
           android:checked="true"
           android:drawableTop="@drawable/selector_home_page"
           android:text="首页"
           android:textColor="@colo

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android Studio中修改底部导航栏字体颜色可以通过设置BottomNavigationView的itemTextColor属性来实现。你可以在布局文件中找到BottomNavigationView的定义,并设置itemTextColor属性为你想要的颜色值。例如,你可以在布局文件中添加以下代码来修改字体颜色: ```xml <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/navigation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" app:itemTextColor="@color/your_color" app:menu="@menu/navigation" /> ``` 其中,@color/your_color是你想要设置的颜色值。你可以在colors.xml文件中定义你想要的颜色值。这样就可以修改底部导航栏的字体颜色了。\[2\] 另外,如果你使用的是viewpager2来实现fragment切换效果,你需要使用setOnItemSelectedListener方法来替代OnNavigationItemSelectedListener方法。你可以在activity中调用该方法,并在方法内部根据选中的item来切换fragment。例如,你可以在activity中添加以下代码来实现fragment切换: ```java private void navigation() { navigation.setOnItemSelectedListener(item -> { switch (item.getItemId()){ case R.id.navigation_shouye: viewPager2.setCurrentItem(0); toolbar.setTitle("首页"); break; case R.id.navigation_xiaoxi: viewPager2.setCurrentItem(1); toolbar.setTitle("消息"); break; case R.id.navigation_biji: viewPager2.setCurrentItem(2); toolbar.setTitle("笔记"); break; case R.id.navigation_my: viewPager2.setCurrentItem(3); toolbar.setTitle("我的"); break; } return true; }); navigation.setSelectedItemId(R.id.navigation_shouye); } ``` 这样就可以实现底部导航栏的字体颜色修改和fragment切换效果了。\[3\] #### 引用[.reference_title] - *1* *2* *3* [Android对于Fragment的使用以及底部导航栏问题](https://blog.csdn.net/qq_43761240/article/details/124517691)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值