android将TabLayout的默认选中效果为圆角(保留点击波纹效果)

先上效果图:

 实现步骤:

1.创建tab后重新设置maskDrawable.setConrnerRadius的值

private TabLayout.Tab newTabView(TabLayout parent, String name) {
        
        TabLayout.Tab tab = parent.newTab().setText(name);
        TabLayout.TabView tabView = tab.view;
        ColorStateList tabRippleColorStateList = parent.getTabRippleColor();
        boolean unboundedRipple=parent.hasUnboundedRipple();

        Drawable background;
        Drawable contentDrawable = new GradientDrawable();
        ((GradientDrawable) contentDrawable).setColor(Color.TRANSPARENT);

        if (tabRippleColorStateList != null) {
            GradientDrawable maskDrawable = new GradientDrawable();
            maskDrawable.setCornerRadius(10F);//重点,参见Tablayout源码修改
            maskDrawable.setColor(Color.WHITE);

            ColorStateList rippleColor =
                    RippleUtils.convertToRippleDrawableColor(tabRippleColorStateList);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                background =
                        new RippleDrawable(
                                rippleColor,
                                unboundedRipple ? null : contentDrawable,
                                unboundedRipple ? null : maskDrawable);
            } else {
                Drawable rippleDrawable = DrawableCompat.wrap(maskDrawable);
                DrawableCompat.setTintList(rippleDrawable, rippleColor);
                background = new LayerDrawable(new Drawable[]{contentDrawable, rippleDrawable});
            }
        } else {
            background = contentDrawable;
        }
        ViewCompat.setBackground(tabView, background);
        parent.invalidate();
        return tab;
    }

2.XML

重点属性

  • app:tabUnboundedRipple 需要为false
  • android:background="@android:color/transparent"
  • app:tabRippleColor="@color/white"
<com.google.android.material.tabs.TabLayout
      android:id="@+id/tabtest"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginStart="72dp"
      android:layout_marginTop="12dp"
      app:tabIndicator="@drawable/tab_indicator"
      app:tabUnboundedRipple="false"
      app:tabRippleColor="@color/white"
      android:background="@android:color/transparent"
      app:tabGravity="center"
      app:tabIndicatorHeight="5dp"
      app:tabMinWidth="98dp"
      app:tabPaddingBottom="7dp"
      app:tabSelectedTextColor="@color/white"
      app:tabTextColor="#8E8E8E"
      app:tabIndicatorColor="#FF634EB7"/>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值