package com.zhaohaoting.framework.ui.utils;

import android.support.design.widget.TabLayout;
import android.text.TextPaint;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.reflect.Field;

/**
 * author：Created by zhaohaoting on 2018/1/10 11:51
 * email：526309416@qq.com
 * desc：
 */
public class TabLayoutUtils {
    /**
     * 设置文字
     *
     * @param tab
     * @param isBold
     */
    public static synchronized boolean setTextBoldStyle(TabLayout.Tab tab, boolean isBold) {
        synchronized (tab) {
            try {
                Field mView = TabLayout.Tab.class.getDeclaredField("mView");
                mView.setAccessible(true);
                Object o = mView.get(tab);
                Field mTextView = o.getClass().getDeclaredField("mTextView");
                mTextView.setAccessible(true);
                TextView textView = (TextView) mTextView.get(o);
                TextPaint tp = textView.getPaint();
                tp.setFakeBoldText(isBold);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
    }

    public static void setTabWidth(final TabLayout tabLayout, final int padding) {
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                try {
                    //拿到tabLayout的mTabStrip属性
                    LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);

                    for (int i = 0; i < mTabStrip.getChildCount(); i++) {
                        View tabView = mTabStrip.getChildAt(i);

                        //拿到tabView的mTextView属性  tab的字数不固定一定用反射取mTextView
                        Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                        mTextViewField.setAccessible(true);

                        TextView mTextView = (TextView) mTextViewField.get(tabView);

                        tabView.setPadding(0, 0, 0, 0);

                        //因为我想要的效果是   字多宽线就多宽，所以测量mTextView的宽度
                        int width = 0;
                        width = mTextView.getWidth();
                        if (width == 0) {
                            mTextView.measure(0, 0);
                            width = mTextView.getMeasuredWidth();
                        }

                        //设置tab左右间距 注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
                        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                        params.width = width;
                        params.leftMargin = padding;
                        params.rightMargin = padding;
                        tabView.setLayoutParams(params);

                        tabView.invalidate();
                    }

                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        });

    }
}
