TabLayout的使用及使用中的问题总结

改变Tab字体颜色设置默认字体颜色:

app:tabTextColor="#ffffff"

设置字体被选中后的颜色

app:tabSelectedTextColor="#e40707"

 设置指示器颜色

app:tabIndicatorColor="#30e407"

 设置指示器高度(如果希望选项卡不显示指示器效果只需要把指示器高度设置为0就行了)

 app:tabIndicatorHeight="2dp"

  设置Tab背景颜色(作用效果和Background一样)

app:tabBackground="@color/colorye"

设置Tab滚动方式

可滚动

app:tabMode="scrollable"

固定

app:tabMode="fixed"

addOnTabSelectedListener

     mytab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
//                添加选中Tab的逻辑
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
//                添加未选中Tab的逻辑
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
//                再次选中tab的逻辑
            }
        });

ViewPager预加载

mViewPager.setOffscreenPageLimit(2);

TabLayout中英文大写改成小写有两种方法:

app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"

或者自己定义一个Style

Button解决英文全是大写的情况是添加一个属性:

textAllCaps="false"

TabLayout在点击Tab的时候会出现阴影的效果,如果想去掉阴影使用一下代码:

app:tabBackground="@color/transparent"
app:tabRippleColor="@color/transparent"

如果在切换Tab的时候想让选中Tab中文字加粗等操作可以使用下面这个Util

public class TabUtils {

    public static void boldTab(TabLayout.Tab tab) {
        setTabStyle(tab, Typeface.DEFAULT_BOLD, 0, 0, 0, 0);
    }

    /**
     * 选中某条, 改变样式
     * @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);
    }

    public static void tabSelectAt(TabLayout tabLayout, TabLayout.Tab currentTab,int position){
        int tabCount = tabLayout.getTabCount();
        TabLayout.Tab tab;
        for (int i = 0; i < tabCount; i++) {
            tab = tabLayout.getTabAt(i);
            if(i != position){
                setTabStyle(tab, Typeface.DEFAULT, 0, 0, 0, 0x00000000);
            }else {
                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;}
        //TODO 暂时不做阴影效果
//        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;
    }

    /**
     * 通过反射去设置下划线样式bufen
     *
     * @param tab
     * @param leftDip
     * @param rightDip
     */
    public static void setIndicator(TabLayout tab, int leftDip, int rightDip) {
        try {
            Field tabStrip = TabLayout.class.getDeclaredField("mTabStrip");
            tabStrip.setAccessible(true);
            LinearLayout llTab = (LinearLayout) tabStrip.get(tab);
            int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
            int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

            for (int i = 0; i < llTab.getChildCount(); i++) {
                View child = llTab.getChildAt(i);
                child.setPadding(0, 0, 0, 0);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
                params.leftMargin = left;
                params.rightMargin = right;
                child.setLayoutParams(params);
                child.invalidate();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 加粗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;
    }
}

在Tab切换的时候监听进行调用:

  mTitleTab.addOnTabSelectedListener(new TabLayout.BaseOnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                //设置选中加粗
                TabUtils.tabSelect(mTitleTab,tab);
            }

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

            }

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

            }
        });

在使用TabLayout的时候,标题的Tab自动换行的解决办法,就是设置也给Tab的宽度。

   app:tabMinWidth="@dimen/space_100"
   app:tabMaxWidth="@dimen/space_200"

 

这就是在使用TabLayout的时候遇到的一些问题及总结,写下来是为了下次更快的找到结果。

你可能感兴趣的:(android,Android,TabLayout,英文大写)