|
1 | 1 | package com.zhy.autolayout.widget; |
2 | 2 |
|
3 | 3 | import android.content.Context; |
| 4 | +import android.content.res.TypedArray; |
4 | 5 | import android.support.v7.widget.Toolbar; |
| 6 | +import android.text.TextUtils; |
5 | 7 | import android.util.AttributeSet; |
| 8 | +import android.util.TypedValue; |
| 9 | +import android.widget.TextView; |
6 | 10 |
|
7 | 11 | import com.zhy.autolayout.AutoLayoutInfo; |
8 | 12 | import com.zhy.autolayout.utils.AutoLayoutHelper; |
| 13 | +import com.zhy.autolayout.utils.AutoUtils; |
| 14 | +import com.zhy.autolayout.utils.DimenUtils; |
| 15 | + |
| 16 | +import java.lang.reflect.Field; |
9 | 17 |
|
10 | 18 | /** |
11 | 19 | * Created by hupei on 2015/12/28 20:33. |
12 | 20 | */ |
13 | 21 | public class AutoToolbar extends Toolbar { |
14 | | - |
| 22 | + private static final int NO_VALID = -1; |
| 23 | + private int mTextSize; |
| 24 | + private int mSubTextSize; |
15 | 25 | private final AutoLayoutHelper mHelper = new AutoLayoutHelper(this); |
16 | 26 |
|
17 | 27 | public AutoToolbar(Context context, AttributeSet attrs, int defStyleAttr) { |
18 | 28 | super(context, attrs, defStyleAttr); |
| 29 | + TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Toolbar, |
| 30 | + defStyleAttr, R.style.Widget_AppCompat_Toolbar); |
| 31 | + |
| 32 | + int titleTextAppearance = a.getResourceId(R.styleable.Toolbar_titleTextAppearance, |
| 33 | + R.style.TextAppearance_Widget_AppCompat_Toolbar_Title); |
| 34 | + |
| 35 | + int subtitleTextAppearance = a.getResourceId(R.styleable.Toolbar_subtitleTextAppearance, |
| 36 | + R.style.TextAppearance_Widget_AppCompat_Toolbar_Subtitle); |
| 37 | + |
| 38 | + mTextSize = loadTextSizeFromTextAppearance(titleTextAppearance); |
| 39 | + mSubTextSize = loadTextSizeFromTextAppearance(subtitleTextAppearance); |
| 40 | + |
| 41 | + TypedArray menuA = context.getTheme().obtainStyledAttributes(attrs, R.styleable.Theme, |
| 42 | + defStyleAttr, R.style.ThemeOverlay_AppCompat); |
| 43 | + int menuTextAppearance = menuA.getResourceId(R.styleable.Theme_actionBarTheme, |
| 44 | + R.style.ThemeOverlay_AppCompat_ActionBar); |
| 45 | + int menuTextSize = loadTextSizeFromTextAppearance(menuTextAppearance); |
| 46 | + |
| 47 | + //防止 menu 定义 textSize,而 Toolbar 无定义 textSize 时,title 的 textSize 随 menu 变化 |
| 48 | + if (mTextSize == NO_VALID) mTextSize = menuTextSize; |
| 49 | + if (mSubTextSize == NO_VALID) mSubTextSize = menuTextSize; |
| 50 | + |
| 51 | + a.recycle(); |
| 52 | + menuA.recycle(); |
19 | 53 | } |
20 | 54 |
|
21 | 55 | public AutoToolbar(Context context, AttributeSet attrs) { |
22 | | - super(context, attrs); |
| 56 | + this(context, attrs, 0); |
23 | 57 | } |
24 | 58 |
|
25 | 59 | public AutoToolbar(Context context) { |
26 | | - super(context); |
| 60 | + this(context, null); |
| 61 | + } |
| 62 | + |
| 63 | + private int loadTextSizeFromTextAppearance(int textAppearanceResId) { |
| 64 | + TypedArray a = getContext().obtainStyledAttributes(textAppearanceResId, |
| 65 | + R.styleable.TextAppearance); |
| 66 | + try { |
| 67 | + if (!DimenUtils.isPxVal(a.peekValue(R.styleable.TextAppearance_android_textSize))) |
| 68 | + return NO_VALID; |
| 69 | + return a.getDimensionPixelSize(R.styleable.TextAppearance_android_textSize, NO_VALID); |
| 70 | + } finally { |
| 71 | + a.recycle(); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private void setUpTitleTextSize() { |
| 76 | + CharSequence title = getTitle(); |
| 77 | + if (!TextUtils.isEmpty(title) && mTextSize != NO_VALID) |
| 78 | + setUpTitleTextSize("mTitleTextView", mTextSize); |
| 79 | + CharSequence subtitle = getSubtitle(); |
| 80 | + if (!TextUtils.isEmpty(subtitle) && mSubTextSize != NO_VALID) |
| 81 | + setUpTitleTextSize("mSubtitleTextView", mSubTextSize); |
| 82 | + } |
| 83 | + |
| 84 | + private void setUpTitleTextSize(String name, int val) { |
| 85 | + try { |
| 86 | + //反射 Toolbar 的 TextView |
| 87 | + Field f = getClass().getSuperclass().getDeclaredField(name); |
| 88 | + f.setAccessible(true); |
| 89 | + TextView textView = (TextView) f.get(this); |
| 90 | + if (textView != null) { |
| 91 | + int autoTextSize = AutoUtils.getPercentHeightSize(val); |
| 92 | + textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, autoTextSize); |
| 93 | + } |
| 94 | + } catch (Exception e) { |
| 95 | + e.printStackTrace(); |
| 96 | + } |
27 | 97 | } |
28 | 98 |
|
29 | 99 | @Override |
30 | 100 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { |
31 | 101 | if (!this.isInEditMode()) { |
| 102 | + setUpTitleTextSize(); |
32 | 103 | this.mHelper.adjustChildren(); |
33 | 104 | } |
34 | 105 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); |
|
0 commit comments