安卓工具类集合 —— 2 TabUtil

本文介绍了一个名为TabUtils的实用工具类,该类在Android开发中用于处理Tab布局和交互。通过使用TabUtils,开发者可以更方便地创建和管理Tab功能。
摘要由CSDN通过智能技术生成

public class TabUtils {

/**
 * 选中某条, 改变样式
 * @param tabLayout
 * @param currentTab
 */
public static void tabSelect(TabLayout tabLayout, TabLayout.Tab currentTab) {
    int tabCount = tabLayout.getTabCount();
    TabLayout.Tab tab;
    for (int i = 0; i < tabCount; i++) {
        tab = tabLayout.getTabAt(i);
        setTabStyle(tab, Typeface.DEFAULT, 0, 0, 0, 0x00000000);
    }

    setTabStyle(currentTab, Typeface.DEFAULT_BOLD, 1, 2, 2, 0x55000000);
}

/**
 * 通过反射去设置样式
 * @param tab
 * @param tf
 * @param radius
 * @param dx
 * @param dy
 * @param color
 */
public static void setTabStyle(TabLayout.Tab tab, Typeface tf, int radius, float dx, float dy, int color) {
    TextView tv = getTextView(tab);
    if (tv == null) { return;}
    tv.setTypeface(tf);
    tv.setShadowLayer(radius, dx, dy, color);
}

private static TextView getTextView(TabLayout.Tab tab){
    try {
        Field mView = tab.getClass().getDeclaredField("mView");
        mView.setAccessible(true);
        Object mViewObj = mView.get(tab);
        Field mTextView = mViewObj.getClass().getDeclaredField("mTextView");
        mTextView.setAccessible(true);
        return (TextView) mTextView.get(mViewObj);
    } catch (Exception e) {

    }

    return null;
}

/**
 * 加粗current
 * @param tabLayout
 * @param currentTab
 */
public static void tabBoldCurrent(TabLayout tabLayout, TabLayout.Tab currentTab) {
    int tabCount = tabLayout.getTabCount();
    TabLayout.Tab tab;
    for (int i = 0; i < tabCount; i++) {
        tab = tabLayout.getTabAt(i);
        TextView tv = getTextView(tab);
        if (tv == null) { continue;}
        tv.setTypeface(Typeface.DEFAULT);
    }

    TextView tv = getTextView(currentTab);
    if (tv == null) { return;}
    tv.setTypeface(Typeface.DEFAULT_BOLD);
}


public static void enableTabs(TabLayout tabLayout, Boolean enable){
    ViewGroup viewGroup = getTabViewGroup(tabLayout);
    if (viewGroup != null)
        for (int childIndex = 0; childIndex < viewGroup.getChildCount(); childIndex++)
        {
            View tabView = viewGroup.getChildAt(childIndex);
            if ( tabView != null)
                tabView.setEnabled(enable);
        }
}

public static View getTabView(TabLayout tabLayout, int position){
    View tabView = null;
    ViewGroup viewGroup = getTabViewGroup(tabLayout);
    if (viewGroup != null && viewGroup.getChildCount() > position)
        tabView = viewGroup.getChildAt(position);

    return tabView;
}

private static ViewGroup getTabViewGroup(TabLayout tabLayout){
    ViewGroup viewGroup = null;

    if (tabLayout != null && tabLayout.getChildCount() > 0 ) {
        View view = tabLayout.getChildAt(0);
        if (view != null && view instanceof ViewGroup)
            viewGroup = (ViewGroup) view;
    }
    return viewGroup;
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值