Android TabLayout 实现随意控制item之间的间距

效果

红色标注是不同的间距。

实现方式

1、xml中定义

kotlin 复制代码
       <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="wrap_content"
            app:tabIndicatorColor="@color/color_FF00B2E3"
            app:tabBackground="@android:color/transparent"
            app:tabRippleColor="@android:color/transparent"
            android:layout_height="wrap_content"
            app:tabIndicatorHeight="5dp"
            android:background="@android:color/transparent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
        </com.google.android.material.tabs.TabLayout>

2、代码动态添加自定义tab

kotlin 复制代码
        for (int i = 0; i < mTabsStrId.length; i++) {
            View tabview = LayoutInflater.from(getContext()).inflate(R.layout.tab_item, null);
            TextView tvTab = tabview.findViewById(R.id.tv_tab);
            tvTab.setText(mTabsStrId[i]);
            TabLayout.Tab tab = binding.tabLayout.newTab();
            tab.setCustomView(tabview);
            binding.tabLayout.addTab(tab, i == 0);
        }

tab的xml布局

kotlin 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_tab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:includeFontPadding="false"
        android:textFontWeight="500"
        android:text="@string/xxx"
        android:textColor="@color/selector_xxx"
        android:textSize="28sp" />
</LinearLayout>

3、去除tablayout原有padding,并设置两个tab之间的间距

kotlin 复制代码
        for (int i = 0; i < binding.tabLayout.getTabCount(); i++) {
            View tab = ((ViewGroup) binding.tabLayout.getChildAt(0)).getChildAt(i);
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) tab.getLayoutParams();
            tab.setPadding(0, 0, 0, 0);
            if (i > 0) {
                params.setMargins(DisplayUtils.dp2px(86), 0, 0, 0); // 设置左右间距
            } else {
                params.setMargins(DisplayUtils.dp2px(22), 0, 0, 0); // 设置左右间距
            }
        }
        binding.tabLayout.invalidate();
相关推荐
敲上瘾6 小时前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
StarsGreen9 小时前
Android 12 中的“域“与“权限“介绍
android
会编程的吕洞宾10 小时前
Spring AI 2.0 工具调用实战:让 AI 自动调用你的 Java 方法
java·人工智能·经验分享·后端·spring
斯维赤10 小时前
SELECT * 为什么是覆盖索引的天敌?
java·后端·mysql
stormsha10 小时前
库存 5,10 个并发各买 1 件:为什么最后不剩 -5 件
java·数据库·mysql
恋猫de小郭10 小时前
Android Keep Rules 解读,直接影响 Google Play 上架和曝光
android·前端·flutter
FlightYe11 小时前
音视频修炼之容器与协议(六):HTTP 协议
android·linux·网络·网络协议·算法·http·音视频
RobinXYuan11 小时前
【Java 字节码探秘】02 ClassFile 骨架:十个字段
java·jvm·源码阅读
炸薯条!11 小时前
Python面向对象基础:类与对象、实例方法、魔法方法和属性详解
java·开发语言·python
SimonKing12 小时前
又一款数据库同步神器,2.3K Star的dbswitch
java·后端·程序员