TabLayout:自定义字体大小

1、主代码 

//方式一:
    private String[] mTabs = new String[]{"案例", "宣传"};
//方式二:动态生成数据
private String[] mTabs = {};
private List<String> firstList = new ArrayList<>();
firstList.add("案例");
firstList.add("宣传");
 mTabs = new String[firstList.size()];
firstList.toArray(mTabs);



private void initTab() {
        addTab(tabLayout, position -> {
           

        });


    }

    private void addTab(TabLayout tabLayout, OnTabChangedListener listener) {
        for (String tab : mTabs) {
            tabLayout.addTab(tabLayout.newTab().setText(tab));
        }
        tabLayout.setScrollableTabRadius(5);
        tabLayout.setSelectedTabIndicatorWidth(70);
        tabLayout.setIndicatorVerticalOffset(0);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {//改变选中状态下文字大小
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setCustomTab(tab, true);
                if (listener != null) {
                    listener.onTabChanged(tab.getPosition());
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                setCustomTab(tab, false);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        TabLayout.Tab tab = tabLayout.getTabAt(0);
        if (tab != null) {
            setCustomTab(tab, true);
         

        }
    }


    private void setCustomTab(TabLayout.Tab tab, boolean isSelected) {
        tab.setCustomView(null);
        int color = Color.parseColor("#999999");
        int size = 15;
        if (isSelected) {
            color = Color.parseColor("#333333");
            size = 18;
        }
        @SuppressLint("InflateParams") TextView textView = (TextView) LayoutInflater.from(context).inflate(R.layout.item_tab_text, null);
        textView.setTextSize(size);
        textView.setTextColor(color);
        textView.setText(tab.getText());
        tab.setCustomView(textView);
    }

  item_tab_text.xml 

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center"
    android:textColor="#333"
    android:textSize="16sp" />

2、自定义接口

public interface OnTabChangedListener {
    void onTabChanged(int position);
}

3、xml

<com.x.common.widget.tablayout.TabLayout
        android:id="@+id/tl_title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#ffffff"
        android:paddingTop="10dp"
        android:paddingBottom="6dp"
        app:tabGravity="center"
        app:tabIndicatorColor="@color/colorPrimary"
        app:tabIndicatorHeight="4dp"
        app:tabPaddingBottom="4dp"
        app:tabSelectedTextColor="@color/colorPrimary" />
<com.x.common.widget.MyTextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:textSize="16sp"
    android:layout_gravity="center"
    android:gravity="center"
    android:textColor="@color/color_333"
    android:layout_width="wrap_content"
    app:tv_bottom_height="15dp"
    app:tv_bottom_width="20dp"
    android:layout_height="wrap_content">

</com.zswl.common.widget.MyTextView>

4、MyTextView

@SuppressLint("AppCompatCustomView")
public class MyTextView extends AppCompatTextView {
    private float mLeftWidth;
    private float mLeftHeight;
    private float mTopWidth;
    private float mTopHeight;
    private float mRightWidth;
    private float mRightHeight;
    private float mBottomWidth;
    private float mBottomHeight;

    public MyTextView(Context context) {
        super(context);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.MyTextView);
        mLeftWidth = t.getDimension(R.styleable.MyTextView_tv_left_width, dip2px(context, 15));
        mLeftHeight = t.getDimension(R.styleable.MyTextView_tv_left_height, dip2px(context, 15));
        mTopWidth = t.getDimension(R.styleable.MyTextView_tv_top_width, dip2px(context, 15));
        mTopHeight = t.getDimension(R.styleable.MyTextView_tv_top_height, dip2px(context, 15));
        mRightWidth = t.getDimension(R.styleable.MyTextView_tv_right_width, dip2px(context, 15));
        mRightHeight = t.getDimension(R.styleable.MyTextView_tv_right_height, dip2px(context, 15));
        mBottomWidth = t.getDimension(R.styleable.MyTextView_tv_bottom_width, dip2px(context, 15));
        mBottomHeight = t.getDimension(R.styleable.MyTextView_tv_bottom_height, dip2px(context, 15));
        t.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //让RadioButton的图标可调大小 属性:
        Drawable drawableLeft = this.getCompoundDrawables()[0];//获得文字左侧图片
        Drawable drawableTop = this.getCompoundDrawables()[1];//获得文字顶部图片
        Drawable drawableRight = this.getCompoundDrawables()[2];//获得文字右侧图片
        Drawable drawableBottom = this.getCompoundDrawables()[3];//获得文字底部图片
        if (drawableLeft != null) {
            drawableLeft.setBounds(0, 0, (int) mLeftWidth, (int) mLeftHeight);
        }
        if (drawableTop != null) {
            drawableTop.setBounds(0, 0, (int) mTopWidth, (int) mTopHeight);
        }
        if (drawableRight != null) {
            drawableRight.setBounds(0, 0, (int) mRightWidth, (int) mRightHeight);
        }
        if (drawableBottom != null) {
            drawableBottom.setBounds(0, 0, (int) mBottomWidth, (int) mBottomHeight);
        }
        this.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    public static int dip2px(Context context, float dpValue) {
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);

    }

    public void setDrawableRight(@DrawableRes int drawableRight) {
        Drawable drawableEnd = getResources().getDrawable(drawableRight);
        int size = dip2px(getContext(), 10);
        drawableEnd.setBounds(0, 0, size, size);
        setCompoundDrawables(null, null, drawableEnd, null);
        setCompoundDrawablePadding(size);
    }

    public void setDrawableRight(@DrawableRes int drawableRight, int size, int padding) {
        Drawable drawableEnd = getResources().getDrawable(drawableRight);
        size = dip2px(getContext(), size);
        drawableEnd.setBounds(0, 0, size, size);
        setCompoundDrawables(null, null, drawableEnd, null);
        setCompoundDrawablePadding(dip2px(getContext(), padding));
    }

    public void setDrawableLeft(@DrawableRes int drawableRight, int size, int padding) {
        Drawable drawableStart = getResources().getDrawable(drawableRight);
        size = dip2px(getContext(), size);
        drawableStart.setBounds(0, 0, size, size);
        setCompoundDrawables(drawableStart, null, null, null);
        setCompoundDrawablePadding(dip2px(getContext(), padding));
    }

    public void setDrawableTop(@DrawableRes int top, int size, int padding) {
        Drawable drawableTop = getResources().getDrawable(top);
        size = dip2px(getContext(), size);
        drawableTop.setBounds(0, 0, size, size);
        setCompoundDrawables(null, drawableTop, null, null);
        setCompoundDrawablePadding(dip2px(getContext(), padding));
    }

    public void setDrawableTop(@DrawableRes int drawableTop) {
        Drawable drawabletop = getResources().getDrawable(drawableTop);
        int size = dip2px(getContext(), 10);
        drawabletop.setBounds(0, 0, size, size);
        setCompoundDrawables(null, drawabletop, null, null);
        setCompoundDrawablePadding(size);
    }

    public void setDrawableBottom(@DrawableRes int bottom, int size, int padding) {
        Drawable drawableBottom = getResources().getDrawable(bottom);
        size = dip2px(getContext(), size);
        drawableBottom.setBounds(0, 0, size, size);
        setCompoundDrawables(null, null, null, drawableBottom);
        setCompoundDrawablePadding(dip2px(getContext(), padding));
    }

    public void setDrawableLeft(@DrawableRes int drawableLeft) {
        Drawable drawableStart = getResources().getDrawable(drawableLeft);
        int size = dip2px(getContext(), 15);
        drawableStart.setBounds(0, 0, size, size);
        setCompoundDrawables(drawableStart, null, null, null);
        setCompoundDrawablePadding(dip2px(getContext(), 5));
    }

    public void setDrawableNone() {
        setCompoundDrawables(null, null, null, null);
    }

}

5。自定义属性

 <declare-styleable name="MyTextView">
        <attr name="tv_left_width" format="dimension" />
        <attr name="tv_left_height" format="dimension" />
        <attr name="tv_top_width" format="dimension" />
        <attr name="tv_top_height" format="dimension" />
        <attr name="tv_right_width" format="dimension" />
        <attr name="tv_right_height" format="dimension" />
        <attr name="tv_bottom_width" format="dimension" />
        <attr name="tv_bottom_height" format="dimension" />
    </declare-styleable>

6、附Tablayout链接

获取Tablayout

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值