一、概述:
目前主流的app一般都会使用到的TabLayout。现在的app设计一般都是定制,定制就意味着个性化。而google提供的tablayout有时候并不能满足我们的需求,还需要用代码一点点的写,这样不仅耗费时间出错率也会更加很多。因此决定封装一些通用的TabLayout来加快这种个性化的tablayout的设计。下面是展示图
二、样例:
使用起来也非常的简单:
a.代码:
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' }