背景
查看官方文档,发现Tablayout的API太少,基本上满足不了现在市场各种奇葩的需求,所以就必须自定义布局了╮(╯▽╰)╭!
过程
废话不多说先上效果图:
- 首先是准备工作
1、MainActivity布局,就一组黄金搭档Tablayout+ViewPager,中间View只是一条分割线:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="0dp"
android:layout_height="40dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:tabBackground="@android:color/transparent"
app:tabIndicatorHeight="0dp"
app:tabMode="scrollable" />
<View
android:layout_width="0dp"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tabLayout"></View>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tabLayout" />
</android.support.constraint.ConstraintLayout>
2、R.layout.item为自定义的Tablayout要显示的样式,这里很简单,只是一个TextView+ImageView(PS:布局根据需求灵活多变,想怎么玩儿就怎么玩儿):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="你好"
android:textColor="@android:color/black"
android:textSize="16sp"
android:textStyle="bold" />
<ImageView
android:visibility="invisible"
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/tv"
android:layout_alignLeft="@+id/tv"
android:layout_alignRight="@+id/tv"
android:layout_centerHorizontal="true"
android:src="@mipmap/img_tablayout_line" />
</RelativeLayout>
- 然后看具体的代码实现
public class MainActivity extends AppCompatActivity {
private String[] title = {"one", "two", "three", "four", "five", "six", "seven"};
private List<Fragment> fragmentList;
private TabLayout tableLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tableLayout = findViewById(R.id.tabLayout);
ViewPager viewPager = findViewById(R.id.viewPager);
fragmentList = new ArrayList<>();
for (int i = 0; i < title.length; i++) {
fragmentList.add(new BlankFragment());
}
viewPager.setAdapter(new Adapter(getSupportFragmentManager()));
tableLayout.setupWithViewPager(viewPager);
getView();
tableLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
setStatus(tab);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
setStatus(tab);
}
});
}
/**
* 获取Tablayout的自定义布局,并设置标题
*/
private void getView() {
for (int i = 0; i < tableLayout.getTabCount(); i++) {
View view = LayoutInflater.from(this).inflate(R.layout.item, null);
((TextView) view.findViewById(R.id.tv)).setText(title[i]);
tableLayout.getTabAt(i).setCustomView(view);
}
((TextView) tableLayout.getTabAt(0).getCustomView().findViewById(R.id.tv)).setTextColor(Color.RED);
tableLayout.getTabAt(0).getCustomView().findViewById(R.id.iv).setVisibility(View.VISIBLE);
}
/**
* 设置tablayout选中状态
*
* @param tab
*/
private void setStatus(TabLayout.Tab tab) {
for (int i = 0; i < tableLayout.getTabCount(); i++) {
if (i == tab.getPosition()) {
((TextView) tableLayout.getTabAt(i).getCustomView().findViewById(R.id.tv)).setTextColor(Color.RED);
tableLayout.getTabAt(i).getCustomView().findViewById(R.id.iv).setVisibility(View.VISIBLE);
} else {
((TextView) tableLayout.getTabAt(i).getCustomView().findViewById(R.id.tv)).setTextColor(Color.BLACK);
tableLayout.getTabAt(i).getCustomView().findViewById(R.id.iv).setVisibility(View.INVISIBLE);
}
}
}
//为了省事儿Adapter写成内部类
class Adapter extends FragmentPagerAdapter {
public Adapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
return title[position];
}
}
}
其中BlankFragment是创建的一个空白的Fragment,其实也没有什么技术难点,就是把自定义的布局setCustomView给Tablayout而已!
- 最后也是一些细节地方需要注意的:
1、在准备工作第1步中 app:tabBackground="@android:color/transparent"是为了Tablayout在点击的时候消除水波纹特效
2、在准备工作第1步中 app:tabMode="scrollable"是为了Tablayout在标题过多的情况下可以横向滚动,其他属性可以查看官方API
总结
这里只是记录总结一下自定义的过程心得,希望可以帮到想要打造炫酷布局的你O(∩_∩)O哈哈~