当前位置: 首页 > 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();
http://www.dtcms.com/a/30856.html

相关文章:

  • 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
  • C++栈与队列:数据结构的“单行道”与“流水线
  • QML Component 与 Loader 结合动态加载组件
  • ES6相关操作
  • Typora的Github主题美化
  • 代码随想录算法训练day59---图论系列4
  • 认识HTML的标签结构
  • OpenCV机器学习(8)随机森林(Random Forests)算法cv::ml::RTrees类
  • 美的楼宇科技基于阿里云 EMR Serverless Spark 构建 LakeHouse 湖仓数据平台
  • Github 2025-02-20 Go开源项目日报 Top10
  • GCC编译器(含预处理/编译/汇编/链接四阶段详解)