TabLayout的tab文字和indicator之间的距离设置

1 设置 TabLayout的 android:layout_height
2 设置 app:tabPaddingBottom

比如说 你的tab文字高度是25 你想设置二者之间的间距是10
那么你android:layout_height的高度就是文本的 高度25dp 加 二者之间的距离10dp
注意不能加上indicator自身的高度5dp 因为indicator的高度是包含在 app:tabPaddingBottom里面的
这样才能实现想要的效果

  android:layout_height="25dp" 
  app:tabPaddingBottom="10dp"
  app:tabIndicatorHeight="5dp"

举个例子

 <jo.android.view.JoTabLayout   // 继承 com.google.android.material.tabs.TabLayout;
                android:id="@+id/home_tab_layout"
                style="@style/JoTabLayout"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:background="#FF0000"
                android:paddingTop="10dp"
                android:paddingBottom="2dp"
                app:joTabs="@string/tabs_news"
                app:tabIndicator="@drawable/video_tab_indicator"
                app:tabIndicatorColor="#ff3d6cfb"
                app:tabIndicatorHeight="4dp"
                app:tabPaddingBottom="8dp" />

这里android:layout_height 设置整体高度45dp
android:paddingTop 10dp
android:paddingBottom=“2dp” + app:tabPaddingBottom=“8dp” 这两个决定了整体的paddingBottom也是10dp
indicator与自定义customView的距离是 app:tabPaddingBottom 减去 app:tabIndicatorHeight的高度
在这里插入图片描述

如果想设置 indicator紧挨着自定义CustomView
那么设置如下

  <jo.android.view.JoTabLayout
                android:id="@+id/home_tab_layout"
                style="@style/JoTabLayout"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:background="#FF0000"
                android:paddingTop="10dp"
                android:paddingBottom="6dp"
                app:joTabs="@string/tabs_news"
                app:tabIndicator="@drawable/video_tab_indicator"
                app:tabIndicatorColor="#ff3d6cfb"
                app:tabIndicatorHeight="4dp"
                app:tabPaddingBottom="4dp" />
  app:tabPaddingBottom="4dp"  这里修改的跟app:tabIndicatorHeight 高度一样就可以了.
    android:paddingBottom="6dp" 保证 6+4 = android:paddingTop
  
  效果图如下

在这里插入图片描述
希望对大家有帮助

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
可以使用自定义View来实现该效果,具体步骤如下: 1. 创建一个自定义的TabLayout,继承自TabLayout。 2. 重写TabLayout的onDraw方法,在该方法中绘制每个tab的边框。 3. 在onMeasure方法中,计算每个tab的宽度,并添加间距。 4. 在TabLayout的addTab方法中,为每个Tab设置自定义的View。 以下是示例代码,供参考: ```java public class CustomTabLayout extends TabLayout { private Paint mPaint; private int mIndicatorColor = Color.RED; private int mSelectedPosition = 0; private int mIndicatorHeight = 5; private int mTabPadding = 20; private int mTabSpacing = 20; public CustomTabLayout(Context context) { super(context); init(); } public CustomTabLayout(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.FILL); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 绘制tab的边框 mPaint.setColor(mIndicatorColor); mPaint.setStrokeWidth(mIndicatorHeight); int tabCount = getTabCount(); for (int i = 0; i < tabCount; i++) { View tabView = getTabAt(i).getCustomView(); if (tabView != null) { int left = tabView.getLeft(); int right = tabView.getRight(); int top = tabView.getTop(); int bottom = tabView.getBottom(); int centerX = (left + right) / 2; if (i == mSelectedPosition) { canvas.drawLine(left, bottom, right, bottom, mPaint); } else { canvas.drawLine(left, bottom, centerX - mTabSpacing / 2, bottom, mPaint); canvas.drawLine(centerX + mTabSpacing / 2, bottom, right, bottom, mPaint); } } } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); // 计算每个tab的宽度,并添加间距 int tabCount = getTabCount(); for (int i = 0; i < tabCount; i++) { View tabView = getTabAt(i).getCustomView(); if (tabView != null) { TextView tabTextView = tabView.findViewById(android.R.id.text1); tabTextView.setPadding(mTabPadding, 0, mTabPadding, 0); int width = tabTextView.getMeasuredWidth() + mTabPadding * 2 + mTabSpacing; tabView.setMinimumWidth(width); } } } @Override public void addTab(@NonNull Tab tab) { super.addTab(tab); // 为每个Tab设置自定义的View View tabView = LayoutInflater.from(getContext()).inflate(R.layout.custom_tab, null); TextView tabTextView = tabView.findViewById(android.R.id.text1); tabTextView.setText(tab.getText()); tab.setCustomView(tabView); } public void setSelectedPosition(int position) { mSelectedPosition = position; invalidate(); } } ``` 其中,自定义的TabLayout中使用了R.layout.custom_tab布局文件来设置每个tab的样式,该布局文件可以根据实际需求自行定义。在该布局文件中,需要使用android.R.id.text1来设置tab的文本。 示例布局文件如下: ```xml <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/tab_text_color" android:textSize="@dimen/tab_text_size" android:textStyle="bold" /> ``` 在使用该自定义TabLayout时,只需要在xml文件中引用即可: ```xml <com.example.CustomTabLayout android:id="@+id/tabLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabIndicatorColor="@color/tab_indicator_color" app:tabSelectedTextColor="@color/tab_text_color" app:tabTextAppearance="@style/CustomTabTextAppearance" app:tabTextColor="@color/tab_text_color" /> ``` 其中,app:tabIndicatorColor、app:tabSelectedTextColor、app:tabTextAppearance、app:tabTextColor等属性与普通TabLayout使用方法一致。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值