Android 开发 TabLayout 自定义指示器长度

devtools/2024/10/24 4:47:32/

前言

原生 TabLayout 的指示器长度是充满整个屏幕的,但在实际开发中 UI 会设计成 指示器的长度等于或者小于标题字体长度,如图

如果设置成跟字体长度一样即使用 API:

mTabLayout.setTabIndicatorFullWidth(false);

或者在 xml 布局文件中的TabLayout标签设置:

 app:tabIndicatorFullWidth=“false”


但如果想要指示器长度小于字体长度(如上图),API并未提供相关方法,此时就需要我们自定义一个CustomTabLayout 继承 TabLayout,允许开发者自定义选项卡的颜色、字体以及背景等属性。

二、自定义 View

public class CustomTabLayout extends TabLayout {private List<String> titles;private int mSelectColor = getResources().getColor(R.color.white);private int mUnSelectColor = getResources().getColor(R.color.c_80ffffff);public CustomTabLayout(Context context) {this(context,null);}public CustomTabLayout(Context context, AttributeSet attrs) {this(context, attrs,0);}public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomTabLayout);mSelectColor = array.getColor(R.styleable.CustomTabLayout_select_color, mSelectColor);mUnSelectColor = array.getColor(R.styleable.CustomTabLayout_unselect_color, mUnSelectColor);array.recycle();init();}private void init() {titles = new ArrayList<>();this.addOnTabSelectedListener(new OnTabSelectedListener() {@Overridepublic void onTabSelected(Tab tab) {/*** 设置当前选中的Tab为特殊高亮样式。*/if (tab != null && tab.getCustomView() != null) {TextView tab_layout_text = tab.getCustomView().findViewById(R.id.tv_tab_layout);View vIndicator = tab.getCustomView().findViewById(R.id.v_indicator);vIndicator.setVisibility(VISIBLE);vIndicator.setBackgroundColor(mSelectColor);tab_layout_text.setTextColor(mSelectColor);}}@Overridepublic void onTabUnselected(Tab tab) {/*** 重置所有未选中的Tab颜色、字体、背景恢复常态(未选中状态)。*/if (tab != null && tab.getCustomView() != null) {TextView tab_layout_text = tab.getCustomView().findViewById(R.id.tv_tab_layout);View vIndicator = tab.getCustomView().findViewById(R.id.v_indicator);vIndicator.setVisibility(INVISIBLE);tab_layout_text.setTextColor(mUnSelectColor);}}@Overridepublic void onTabReselected(Tab tab) {}});}public void setTitle(List<String> titles) {this.titles = titles;/*** 开始添加切换的Tab。*/for (String title : this.titles) {Tab tab = newTab();tab.setCustomView(R.layout.item_custom_tablayout);if (tab.getCustomView() != null) {TextView text = tab.getCustomView().findViewById(R.id.tv_tab_layout);text.setText(title);text.setTextColor(mUnSelectColor);}this.addTab(tab);}}
}

相关属性

styleable 
    <declare-styleable name="CustomTabLayout"><attr name="select_color" format="color"/><attr name="unselect_color" format="color"/></declare-styleable>
item_custom_tablayout
<?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"android:paddingBottom="@dimen/dimen_5"android:gravity="center_horizontal"><TextViewandroid:id="@+id/tv_tab_layout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textStyle="bold"android:textColor="@color/c_80ffffff"android:textSize="@dimen/textsize_16"/><Viewandroid:id="@+id/v_indicator"android:layout_width="@dimen/dimen_30"android:layout_height="@dimen/dimen_3"android:visibility="invisible"android:layout_marginTop="@dimen/dimen_6"android:background="@color/white"/></LinearLayout>

使用方法

List<String> titles = new ArrayList<>();
titles.add("待签收");
titles.add("已签收");List<Fragment> mFragments = initFragments();
adapter = new ViewPaperAdapter(getSupportFragmentManager(), mFragments, titles);
mViewPager.setAdapter(adapter);mTabLayout.setTitle(titles);//Tablayout自定义view绑定ViewPager
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(mTabLayout));
mTabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));

总结

如果对你有所帮助的话,不妨 点赞收藏
如果你有什么疑问的话,不妨 评论私信
青山不改,绿水长流 ,有缘江湖再见 ~


http://www.ppmy.cn/devtools/128362.html

相关文章

无人机封闭空间建图检测系统技术详解

无人机封闭空间建图检测系统技术是一种集成了多种传感器、智能算法与控制系统的高级技术&#xff0c;旨在实现无人机在复杂封闭环境下的自主导航、精确建图与高效检测。以下是对该技术的详细解析&#xff1a; 一、技术概述 无人机封闭空间建图检测系统通过集成激光雷达(LiDAR…

【设计模式-迪米特法则】

迪米特法则&#xff08;Law of Demeter&#xff0c;LoD&#xff09;&#xff0c;也称为最少知识原则&#xff08;Principle of Least Knowledge&#xff09;&#xff0c;是一种面向对象编程中的设计原则。它的核心思想是&#xff1a;一个对象应当尽可能少地了解其他对象&#x…

基于STM32的水温控制系统设计

引言 水温控制系统在家庭、工业和农业等多个领域具有重要应用&#xff0c;尤其在加热设备、热水器和地暖系统中&#xff0c;通过实时监测和调节水温可以提高能源利用效率&#xff0c;保证使用的舒适性和安全性。本文基于STM32设计了一个智能水温控制系统&#xff0c;集成温度传…

调查显示软件供应链攻击增加

OpenText 发布了《2024 年全球勒索软件调查》&#xff0c;强调了网络攻击的重要趋势&#xff0c;特别是在软件供应链中&#xff0c;以及生成式人工智能在网络钓鱼诈骗中的使用日益增多。 尽管各国政府努力加强网络安全措施&#xff0c;但调查显示&#xff0c;仍有相当一部分企…

scrapy案例——豆瓣电影Top250的爬取

项目&#xff1a;豆瓣电影Top250的爬取 案例需求&#xff1a; 1.使用scrapy爬虫技术爬取豆瓣电影Top250的电影数据&#xff08;包括排名、电影名称、导演、演员、url、评分&#xff09; 2.实现分页爬取&#xff0c;共十页 3.将爬取下来的数据保存在数据库中 案例分析&…

初学者怎么入门大语言模型(LLM)?

如果你真心想要入门大型语言模型&#xff08;LLM&#xff09;&#xff0c;只看这一个文章应该是可以入门的。但是修行下去&#xff0c;还是要靠自己的了&#xff01; 如果你把大语言模型/LLM 当成一门技术来看&#xff0c;那就要看一下这门技术需要什么。 基本要求&#xff1…

给哔哩哔哩bilibili电脑版做个手机遥控器

前言 bilibili电脑版可以在电脑屏幕上观看bilibili视频。然而&#xff0c;电脑版的bilibili不能通过手机控制视频翻页和调节音量&#xff0c;这意味着观看视频时需要一直坐在电脑旁边。那么&#xff0c;有没有办法制作一个手机遥控器来控制bilibili电脑版呢&#xff1f; 首先…

pdf文件怎样一张纸打印四页

在日常工作和学习中&#xff0c;我们经常会遇到需要将PDF文件中的多页内容合并打印到一张纸上的情况&#xff0c;比如将四页内容打印到一张A4纸上&#xff0c;以节省纸张和成本。同时&#xff0c;在打开pdf文件的方式&#xff0c;一般都是通过电脑浏览器打印&#xff0c;因此对…