当前位置: 首页 > news >正文

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

效果

红色标注是不同的间距。
在这里插入图片描述

实现方式

1、xml中定义

       <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

        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布局

<?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之间的间距

        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();

相关文章:

  • rk3588/3576板端编译程序无法运行视频推理
  • vue-element-admin 打包部署到SpringBoot
  • Linux Python 调试/堵塞/性能分析与定位工具
  • 【Cesium学习(十三)】Cesium学习主要优秀资源资料总结
  • python用 PythonNet 从 Python 调用 WPF 类库 UI 用XAML
  • 支持向量机 (Support Vector Machine, SVM)
  • ProfiNet转EtherNet/IP罗克韦尔PLC与监控系统通讯案例
  • hydra docker版本
  • 云原生监控体系建设:Kubernetes架构下的全面监控策略
  • DeepSeek R1本地Linux服务器Docker部署<实现网页访问/本地终端访问>完整教程
  • vxe-grid 通过配置式给单元格字段格式化树结构数据,转换树结构节点
  • CentOS7设置静态IP
  • 细分数字货币钱包的不同种类
  • CSS文本属性
  • 网工项目实践2.4 北京公司安全加固、服务需求分析及方案制定
  • CSS基础(浮动、相对定位、绝对定位、固定定位、粘性定位、版心、重置默认样式)
  • 22爬虫:使用Drission Page的两个案例
  • 网络安全高级软件编程技术
  • Java八股文(下)
  • Mac安装配置Tomcat 8
  • 巴基斯坦副总理兼外长达尔将访华
  • 李公明 | 一周画记:德里达哲学还是接地气的
  • 西浦国际教育创新论坛举行,聚焦AI时代教育本质的前沿探讨
  • 江苏疾控:下设部门无“病毒研究所”,常荣山非本单位工作人员
  • 义乌至迪拜“铁海快线+中东快航”首发,物流成本降低18%
  • 广东缉捕1名象牙走私潜逃非洲“红通”逃犯