TabLayout
TabLayout设置选中字体变大,加粗,透明度
TabLayout自带没有设置选中时字体大小的属性,布局要自己定义下,要不不好使。
<com.google.android.material.tabs.TabLayout
android:id="@+id/tablayout"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_alignParentTop="true"
android:layout_toStartOf="@id/back_container"
android:layout_toEndOf="@id/mine_container"
app:tabGravity="fill"
app:tabIndicatorColor="#ffffff"
app:tabMode="fixed"
app:tabIndicatorHeight="2dp"
app:tabIndicatorFullWidth="false"
app:tabTextColor="#ffffff" />
java文件中关键代码,直接看addOnTabSelectedListener就行。
public TextView toMyTextView;
public TextView toBeReceivedTextView;
public void initView(View v) {
//此处省略一万行
tablayout.setupWithViewPager(mPager);
tablayout.getTabAt(0).setCustomView(R.layout.main_top_item);
toMyTextView = tablayout.getTabAt(0).getCustomView().findViewById(R.id.tv_top_item);
tablayout.getTabAt(1).setCustomView(R.layout.main_top_item);
toBeReceivedTextView = tablayout.getTabAt(1).getCustomView().findViewById(R.id.tv_top_item);
tablayout.setTabRippleColor(ColorStateList.valueOf(getContext().getResources().getColor(R.color.transparent)));/*去除tablayout 子tab点击时的黑色背景*/
//默认选择第一个tab,设置字体大小和默认风格为加粗 toMyTextView是我自己项目中第一个Tab的TextView,自己看着改。
toMyTextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
toMyTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);
//看这里看这里看这里
tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
tab.getCustomView().findViewById(R.id.tv_top_item).setSelected(true);
TextView tv = tab.getCustomView().findViewById(R.id.tv_top_item);
tv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));//加粗
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 22);//直接用setTextSize(22)也一样
tv.setAlpha(0.9f);//透明度
tv.invalidate();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
tab.getCustomView().findViewById(R.id.tv_top_item).setSelected(false);
TextView tv = tab.getCustomView().findViewById(R.id.tv_top_item);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
tv.setAlpha(0.6f);
tv.invalidate();
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
自定义布局main_top_item.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_top_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:singleLine="true"
android:textSize="@dimen/txtsize18sp"
android:textColor="@color/white"
android:gravity="center"/>
</RelativeLayout>
https://www.jianshu.com/p/cdc57e6f48f0
资料
https://www.jianshu.com/p/b9e349a9cfcc
https://www.cnblogs.com/zxxiaoxia/p/10614058.html
开源项目
https://github.com/Ro6Fish/FlycoTabLayout
TabLayout禁止选择
项目中有个页面上面是TabLayout下面是Listview,选择TabLayout的选项卡更新下面Listview里面的数据,在请求的时候想禁用TabLayout选项卡来避免用户频繁点击选项卡造成Listview的数据错误。 如果只是调用TabLayout的setClickable方法是不起作用的,需要获取到每个选项卡的tabView然后再分别设置。
java
LinearLayout tabStrip = (LinearLayout) tabLayout.getChildAt(0);
for (int i = 0; i < tabStrip.getChildCount(); i++) {
View tabView = tabStrip.getChildAt(i);
if (tabView != null) {
tabView.setClickable(false);
}
}
kotlin
val tabStrip = tl_tabs.getChildAt(0) as LinearLayout
for (i in 0 until tabStrip.childCount) {
val tabView = tabStrip.getChildAt(i)
if (tabView != null) {
tabView.isClickable = false
}
}