android 字体选中加粗,Android——TabLayout设置选中字体变大,加粗,透明度

TabLayout自带没有设置选中时字体大小的属性,网上搜了好多基本都不生效,搞了一上午终于好使了,记个笔记记录下,布局要自己定义下,要不不好使。

布局中使用TabLayout的xml文件

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

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">

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"/>

验证OK,撒花撒花。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值