|
| 1 | +package com.zhy.autolayout.widget; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.res.TypedArray; |
| 5 | +import android.support.design.widget.TabLayout; |
| 6 | +import android.util.AttributeSet; |
| 7 | +import android.util.TypedValue; |
| 8 | +import android.view.ViewGroup; |
| 9 | +import android.widget.TextView; |
| 10 | + |
| 11 | +import com.zhy.autolayout.utils.AutoUtils; |
| 12 | +import com.zhy.autolayout.utils.DimenUtils; |
| 13 | + |
| 14 | +/** |
| 15 | + * Created by zhy on 16/3/3. |
| 16 | + */ |
| 17 | +public class AutoTabLayout extends TabLayout |
| 18 | +{ |
| 19 | + private static final int NO_VALID = -1; |
| 20 | + private int mTextSize; |
| 21 | + private boolean mTextSizeBaseWidth = false; |
| 22 | + |
| 23 | + public AutoTabLayout(Context context) |
| 24 | + { |
| 25 | + this(context, null); |
| 26 | + } |
| 27 | + |
| 28 | + public AutoTabLayout(Context context, AttributeSet attrs) |
| 29 | + { |
| 30 | + this(context, attrs, 0); |
| 31 | + } |
| 32 | + |
| 33 | + public AutoTabLayout(Context context, AttributeSet attrs, int defStyleAttr) |
| 34 | + { |
| 35 | + super(context, attrs, defStyleAttr); |
| 36 | + |
| 37 | + initTextSizeBaseWidth(context, attrs); |
| 38 | + |
| 39 | + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TabLayout, |
| 40 | + defStyleAttr, R.style.Widget_Design_TabLayout); |
| 41 | + int tabTextAppearance = a.getResourceId(R.styleable.TabLayout_tabTextAppearance, |
| 42 | + R.style.TextAppearance_Design_Tab); |
| 43 | + |
| 44 | + mTextSize = loadTextSizeFromTextAppearance(tabTextAppearance); |
| 45 | + a.recycle(); |
| 46 | + } |
| 47 | + |
| 48 | + private void initTextSizeBaseWidth(Context context, AttributeSet attrs) |
| 49 | + { |
| 50 | + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AutoTabLayout); |
| 51 | + mTextSizeBaseWidth = a.getBoolean(R.styleable.AutoTabLayout_auto_textSize_base_width, false); |
| 52 | + a.recycle(); |
| 53 | + } |
| 54 | + |
| 55 | + private int loadTextSizeFromTextAppearance(int textAppearanceResId) |
| 56 | + { |
| 57 | + TypedArray a = getContext().obtainStyledAttributes(textAppearanceResId, |
| 58 | + R.styleable.TextAppearance); |
| 59 | + |
| 60 | + try |
| 61 | + { |
| 62 | + if (!DimenUtils.isPxVal(a.peekValue(R.styleable.TextAppearance_android_textSize))) |
| 63 | + return NO_VALID; |
| 64 | + return a.getDimensionPixelSize(R.styleable.TextAppearance_android_textSize, NO_VALID); |
| 65 | + } finally |
| 66 | + { |
| 67 | + a.recycle(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void addTab(Tab tab, int position, boolean setSelected) |
| 73 | + { |
| 74 | + super.addTab(tab, position, setSelected); |
| 75 | + setUpTabTextSize(tab); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void addTab(Tab tab, boolean setSelected) |
| 80 | + { |
| 81 | + super.addTab(tab, setSelected); |
| 82 | + setUpTabTextSize(tab); |
| 83 | + } |
| 84 | + |
| 85 | + private void setUpTabTextSize(Tab tab) |
| 86 | + { |
| 87 | + if (mTextSize == NO_VALID || tab.getCustomView() != null) return; |
| 88 | + |
| 89 | + ViewGroup tabGroup = (ViewGroup) getChildAt(0); |
| 90 | + ViewGroup tabContainer = (ViewGroup) tabGroup.getChildAt(tab.getPosition()); |
| 91 | + TextView textView = (TextView) tabContainer.getChildAt(1); |
| 92 | + |
| 93 | + if (AutoUtils.autoed(textView)) |
| 94 | + { |
| 95 | + return; |
| 96 | + } |
| 97 | + if (mTextSizeBaseWidth) |
| 98 | + { |
| 99 | + mTextSize = AutoUtils.getPercentWidthSize(mTextSize); |
| 100 | + } else |
| 101 | + { |
| 102 | + mTextSize = AutoUtils.getPercentHeightSize(mTextSize); |
| 103 | + } |
| 104 | + textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize); |
| 105 | + } |
| 106 | + |
| 107 | + |
| 108 | +} |
0 commit comments