最近做项目有一个需求,就是一个选项卡的tab中的字体需要设置选中的字体变大,但是smarttablayout中并没有这个属性,又不想重新写一个新的tab,所以就想办法简单改造一下。
首先献上smarttablayout的地址:https://github.com/ogaclejapan/SmartTabLayout
1.编写xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<com.ogaclejapan.smarttablayout.SmartTabLayout
android:id="@+id/viewpagertab"
android:layout_width="match_parent"
android:layout_height="48dp"
app:stl_clickable="true"
android:background="@color/white"
app:stl_dividerColor="@color/transparent"
app:stl_dividerThickness="0dp"
app:stl_indicatorColor="@color/transparent"
app:stl_indicatorThickness="0dp"
app:stl_underlineColor="@color/transparent"
app:stl_underlineThickness="0dp"
/>
<androidx.viewpager.widget.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/viewpagertab" />
</LinearLayout>
2.在activity中使用:
@BindView(R.id.viewpagertab)
SmartTabLayoutviewpagertab;
@BindView(R.id.viewpager)
ViewPagerviewpager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LayoutInflater inflater = LayoutInflater.from(this);
final int[] tabTitles = {R.string.shop_dynamic, R.string.shop_message};
FragmentPagerItemAdapter adapter =new FragmentPagerItemAdapter(
getSupportFragmentManager(), FragmentPagerItems.with(this)
.add(R.string.shop_dynamic, ShopDynamicFragment.class)
.add(R.string.shop_message, ShopMessageFragment.class)
.create());
viewpager.setAdapter(adapter);
viewpagertab.setCustomTabView(new SmartTabLayout.TabProvider() {
@Override
public ViewcreateTabView(ViewGroup container, int position, PagerAdapter adapter) {
View view =inflater.inflate(R.layout.widget_tab_text_item, container, false);
TextView titleView = (TextView) view.findViewById(R.id.txt_title);
titleView.setText(tabTitles[position %tabTitles.length]);
return view;
}
});
viewpagertab.setOnTabClickListener(new SmartTabLayout.OnTabClickListener() {
@Override
public void onTabClicked(int position) {
}
});
viewpagertab.setViewPager(viewpager);
viewpager.setCurrentItem(mIndex);
}
3.widget_tab_text_item:
<?xml version="1.0" encoding="utf-8"?>
<com.arounds.block.widgets.texts.SizeTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_micro"
android:textColor="@color/navigation_bottom_text_color"
>
</com.arounds.block.widgets.texts.SizeTextView>
4.重点是自定义的textview:
/**
* create by StoneWay on 2020/3/7
*/
public class SizeTextViewextends TextView {
public SizeTextView(Context context) {
super(context);
}
public SizeTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public SizeTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void initSize() {
if (isSelected()) {
setTextSize(18);
setTypeface(Typeface.DEFAULT,Typeface.BOLD);
}else {
setTextSize(14);
setTypeface(Typeface.SANS_SERIF,Typeface.NORMAL );
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
initSize();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
5.ok,这样就可以改变字体大小了,看一下效果:
6,你还可以根据这个思路,做其他的效果