BottomNavigationView + ViewPager2 实现底部导航栏切换 + 自定义渐变

  1. com.google.android.material.bottomnavigation.BottomNavigationView 缺点:

自定义难度大:

BottomNavigationView 的默认样式和行为是为标准使用场景设计的,如果需要进行深度定制,比如复杂的动画效果或不常见的布局,可能需要大量的代码来实现。

图标和文字的限制:

默认情况下,BottomNavigationView 中的每个菜单项同时显示图标和文字,虽然可以通过自定义布局来改变这种行为,但有时候会影响到视觉效果或用户体验。

项目数限制:

BottomNavigationView 适合展示 3 到 5 个菜单项,超过这个数量会导致布局变得拥挤,图标和文字也会变得太小,不利于用户点击和查看。

动画和交互效果有限:

默认的交互动画和效果比较基础,如果需要更复杂或不同的动画效果,可能需要自己实现,而这可能会增加开发时间和复杂性。

适应性问题:

BottomNavigationView 对大多数屏幕尺寸和分辨率有良好的适应性,但在一些特殊设备或屏幕方向(如横屏)下,可能需要额外的调整才能保证良好的显示效果。

版本兼容性:

BottomNavigationView 是 Material Design 组件库的一部分,对于较旧版本的 Android 系统,需要确保兼容性,特别是在使用一些新特性或修复一些 bug 时,可能需要额外的工作。
这些缺点并不是说 BottomNavigationView 不好用,对于大多数标准的应用场景,它依然是一个非常好的选择。

以下针对fragment的切换和自定义tab文字渐变来说明如何修改:
先看原始布局xml –

<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">

    <fragment
        android:id="@+id/xxx_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/xxx_nav_main" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf
  • 30
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值