最近做項(xiàng)目有一個(gè)需求宠纯,就是一個(gè)選項(xiàng)卡的tab中的字體需要設(shè)置選中的字體變大卸夕,但是smarttablayout中并沒有這個(gè)屬性,又不想重新寫一個(gè)新的tab婆瓜,所以就想辦法簡單改造一下快集。
首先獻(xiàn)上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.重點(diǎn)是自定義的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,你還可以根據(jù)這個(gè)思路个初,做其他的效果