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

news/2025/2/22 18:53:37

效果

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

实现方式

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.niftyadmin.cn/n/5862649.html

相关文章

Spring Boot项目@Cacheable注解的使用

Cacheable 是 Spring 框架中用于缓存的注解之一&#xff0c;它可以帮助你轻松地将方法的结果缓存起来&#xff0c;从而提高应用的性能。下面详细介绍如何使用 Cacheable 注解以及相关的配置和注意事项。 1. 基本用法 1.1 添加依赖 首先&#xff0c;确保你的项目中包含了 Spr…

1. Linux下 MySQL 的详细安装与使用

1. Linux下 MySQL 的详细安装与使用 文章目录 1. Linux下 MySQL 的详细安装与使用1. Linux 下安装 MySQL8.0 的详细安装步骤&#xff1a;2. Linxu 当中的MySQL 设置远程登录3. 最后&#xff1a; 1. Linux 下安装 MySQL8.0 的详细安装步骤&#xff1a; 查看是否安装过MySQL&…

正则表达式常用记录

1. 定义 正则表达式&#xff0c;又称规则表达式,&#xff08;Regular Expression&#xff0c;在代码中常简写为regex、regexp或RE&#xff09;&#xff0c;它是一种文本模式&#xff0c;同时也是计算机科学的一个概念&#xff0c;其中包括普通字符&#xff08;例如&#xff0c…

图数据库Neo4j面试内容整理-模式匹配

模式匹配 是图数据库中一个重要的概念,尤其是在 Neo4j 中,它允许用户通过查询语言(如 Cypher)定义节点和关系之间的特定结构或模式,并查找符合该模式的图数据。模式匹配使得你能够通过图的结构来表达复杂的查询条件。 在 Neo4j 中,模式匹配通常用于查询图中节点、关系和它…

并查集算法篇上期:并查集原理及实现

引入 那么我们在介绍我们并查集的原理之前&#xff0c;我们先来看一下并查集所应用的一个场景&#xff1a;那么现在我们有一个长度为n的数组&#xff0c;他们分别属于不同的集合&#xff0c;那么现在我们要查询数组当中某个元素和其他元素是否处于同一集合当中&#xff0c;或者…

Hive Orc表数据导出和导入

导出到hdfs&#xff1a;hive执行 INSERT OVERWRITE DIRECTORY /test/hdfs_dir ROW FORMAT DELIMITED FIELDS TERMINATED BY \t STORED AS ORC SELECT * FROM hive_table; HDFS导出到本地&#xff1a;shell执行 hdfs dfs -get /test/hdfs_dis/file_name /linux_dir/xxx 本…

EP零散笔记

ToF技术&#xff1a;计算出传感器和障碍物之间的距离 -- eg&#xff1a;红外深度传感器(测距传感器)使用的就是ToF技术 chassic_ctrl.move_with_distance(0,1) #底盘向0移动1m gun_ctrl.fire_once() #单次发射子弹 #gun射击 #设置整机运动 robot.set_m…

NSFNET是什么?NSFNET网络具有什么特点?

NSFNET网络特点 NSFNET&#xff08;National Science Foundation Network&#xff0c;美国国家科学基金会网络&#xff09;是美国国家科学基金会&#xff08;NSF&#xff09;在 20 世纪 80 年代建立的计算机网络&#xff0c;在互联网发展历程中扮演了重要角色&#xff0c;具体…