Android 通用的TabLayout组件
一、概述:
目前主流的app一般都会使用到的TabLayout。现在的app设计一般都是定制,定制就意味着个性化。而google提供的tablayout有时候并不能满足我们的需求,还需要用代码一点点的写,这样不仅耗费时间出错率也会更加很多。因此决定封装一些通用的TabLayout来加快这种个性化的tablayout的设计。下面是展示图
二、样例:
使用起来也非常的简单:
a.代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | setContentView(R.layout.selecttablayout); CommonTabLayout selectTabLayout = (CommonTabLayout) findViewById(R.id.tablayout); List<String> datas = new ArrayList<>(); datas.add( "Item1" ); datas.add( "Item2" ); datas.add( "Item3" ); datas.add( "Item4" ); datas.add( "Item5" ); /** * 创建一个左、中、右背景切换各不同的TabLayout(ps:也可以相同,看您怎样设置) */ selectTabLayout. setData(datas). setTextColor(R.color.manager_radio_textcolor_selector). setLeftDrawableSelector(R.drawable.manager_radio_left_selector). setMiddleDrawableSelector(R.drawable.manager_radio_middle_selector). setRightDrawableSelector(R.drawable.manager_radio_right_selector). setDefaultSelectIndex( 2 ). setOnItemClickListener( new CommonTabLayout.OnItemClickListener() { @Override public void onItemClick( int pos) { Toast.makeText(CommonTabLayoutActivity. this , "您点击了其中的某一项:" +pos, Toast.LENGTH_SHORT).show(); } }).buildViewByLeftMiddleRight(); CommonTabLayout selectTabLayout2 = findViewById(R.id.tablayout2); List<String> datas2 = new ArrayList<>(); datas2.add( "Left" ); datas2.add( "Right" ); /** * 创建一个左、右背景切换各不同的TabLayout(ps:也可以相同,看您怎样设置) */ selectTabLayout2. setData(datas2). setTextColor(R.color.manager_radio_textcolor_selector). setLeftDrawableSelector(R.drawable.manager_radio_left_selector). setMiddleDrawableSelector(R.drawable.manager_radio_middle_selector). setRightDrawableSelector(R.drawable.manager_radio_right_selector). setDefaultSelectIndex( 1 ). setOnItemClickListener( new CommonTabLayout.OnItemClickListener() { @Override public void onItemClick( int pos) { Toast.makeText(CommonTabLayoutActivity. this , "您点击了其中的某一项:" +pos, Toast.LENGTH_SHORT).show(); } }).buildViewByLeftRight(); |
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <com.yw.ywlibrary.widget.tab.CommonTabLayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_margin="20dp" android:layout_height="wrap_content"/> <com.yw.ywlibrary.widget.tab.CommonTabLayout android:id="@+id/tablayout2" android:layout_width="match_parent" android:layout_margin="20dp" android:layout_height="wrap_content"/> <com.yw.ywlibrary.widget.tab.CommonTabLayout android:id="@+id/tablayout3" android:layout_width="match_parent" android:layout_margin="20dp" android:layout_height="wrap_content"/> <com.yw.ywlibrary.widget.tab.CommonTabLayout android:id="@+id/tablayout4" android:layout_width="match_parent" android:layout_margin="20dp" android:layout_height="wrap_content"/> <com.yw.ywlibrary.widget.tab.CommonTabLayout android:id="@+id/tablayout5" android:layout_width="match_parent" android:layout_margin="20dp" android:layout_height="wrap_content"/> </LinearLayout>
c.源代码下载地址:github
d.直接引入路径:
allprojects { repositories { ... maven { url 'https://jitpack.io' } } } Step 2. Add the dependency dependencies { implementation 'com.github.ywtony:customerweight:Tag' }
分类:
Android
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探