11package com .qmuiteam .qmui .widget ;
22
33import android .content .Context ;
4+ import android .content .res .ColorStateList ;
45import android .content .res .TypedArray ;
56import android .graphics .Color ;
67import android .graphics .Rect ;
@@ -59,10 +60,19 @@ public class QMUITopBar extends RelativeLayout {
5960
6061 private int mTitleGravity ;
6162 private int mLeftBackDrawableRes ;
63+ private int mTitleTextSize ;
64+ private int mTitleTextSizeWithSubTitle ;
65+ private int mSubTitleTextSize ;
66+ private int mTitleTextColor ;
67+ private int mSubTitleTextColor ;
68+ private int mTitleMarginHorWhenNoBtnAside ;
69+ private int mTitleContainerPaddingHor ;
70+ private int mTopBarImageBtnWidth ;
71+ private int mTopBarImageBtnHeight ;
72+ private int mTopBarTextBtnPaddingHor ;
73+ private ColorStateList mTopBarTextBtnTextColor ;
74+ private int mTopBarTextBtnTextSize ;
6275 private int mTopbarHeight = -1 ;
63- private int mTopbarImageBtnWidth = -1 ;
64- private int mTopbarImageBtnHeight = -1 ;
65- private int mTopbarTextBtnPaddingHorizontal = -1 ;
6676 private Rect mTitleContainerRect ;
6777
6878 public QMUITopBar (Context context , AttributeSet attrs , int defStyleAttr ) {
@@ -85,18 +95,16 @@ public QMUITopBar(Context context) {
8595 // ========================= title 相关的方法
8696
8797 // 这个构造器只用于QMUI内部,不开放给外面用,目前用于QMUITopBarLayout
88- QMUITopBar (Context context , boolean inTopBarLayout , int leftBackDrawableRes ) {
98+ QMUITopBar (Context context , boolean inTopBarLayout ) {
8999 super (context );
90100 initVar ();
91101 if (inTopBarLayout ) {
92102 int transparentColor = ContextCompat .getColor (context , R .color .qmui_config_color_transparent );
93103 mTopBarSeparatorColor = transparentColor ;
94104 mTopBarSeparatorHeight = 0 ;
95- mLeftBackDrawableRes = leftBackDrawableRes ;
96105 mTopBarBgColor = transparentColor ;
97106 } else {
98107 init (context , null , R .attr .QMUITopBarStyle );
99- mLeftBackDrawableRes = leftBackDrawableRes ;
100108 }
101109 }
102110
@@ -113,14 +121,31 @@ private void init(Context context, AttributeSet attrs, int defStyleAttr) {
113121 ContextCompat .getColor (context , R .color .qmui_config_color_separator ));
114122 mTopBarSeparatorHeight = array .getDimensionPixelSize (R .styleable .QMUITopBar_qmui_topbar_separator_height , 1 );
115123 mTopBarBgColor = array .getColor (R .styleable .QMUITopBar_qmui_topbar_bg_color , Color .WHITE );
116- mLeftBackDrawableRes = array .getResourceId (R .styleable .QMUITopBar_qmui_topbar_left_back_drawable_id , R .id .qmui_topbar_item_left_back );
117- mTitleGravity = array .getInt (R .styleable .QMUITopBar_qmui_topbar_title_gravity , Gravity .CENTER );
124+ getCommonFieldFormTypedArray (context , array );
118125 boolean hasSeparator = array .getBoolean (R .styleable .QMUITopBar_qmui_topbar_need_separator , true );
119126 array .recycle ();
120127
121128 setBackgroundDividerEnabled (hasSeparator );
122129 }
123130
131+ void getCommonFieldFormTypedArray (Context context , TypedArray array ) {
132+ mLeftBackDrawableRes = array .getResourceId (R .styleable .QMUITopBar_qmui_topbar_left_back_drawable_id , R .id .qmui_topbar_item_left_back );
133+ mTitleGravity = array .getInt (R .styleable .QMUITopBar_qmui_topbar_title_gravity , Gravity .CENTER );
134+ mTitleTextSize = array .getDimensionPixelSize (R .styleable .QMUITopBar_qmui_topbar_title_text_size , QMUIDisplayHelper .sp2px (context , 17 ));
135+ mTitleTextSizeWithSubTitle = array .getDimensionPixelSize (R .styleable .QMUITopBar_qmui_topbar_title_text_size , QMUIDisplayHelper .sp2px (context , 16 ));
136+ mSubTitleTextSize = array .getDimensionPixelSize (R .styleable .QMUITopBar_qmui_topbar_subtitle_text_size , QMUIDisplayHelper .sp2px (context , 11 ));
137+ mTitleTextColor = array .getColor (R .styleable .QMUITopBar_qmui_topbar_title_color , QMUIResHelper .getAttrColor (context , R .attr .qmui_config_color_gray_1 ));
138+ mSubTitleTextColor = array .getColor (R .styleable .QMUITopBar_qmui_topbar_subtitle_color , QMUIResHelper .getAttrColor (context , R .attr .qmui_config_color_gray_4 ));
139+ mTitleMarginHorWhenNoBtnAside = array .getDimensionPixelSize (R .styleable .QMUITopBar_qmui_topbar_title_margin_horizontal_when_no_btn_aside , 0 );
140+ mTitleContainerPaddingHor = array .getDimensionPixelSize (R .styleable .QMUITopBar_qmui_topbar_title_container_padding_horizontal , 0 );
141+ mTopBarImageBtnWidth = array .getDimensionPixelSize (R .styleable .QMUITopBar_qmui_topbar_image_btn_width , QMUIDisplayHelper .dp2px (context , 48 ));
142+ mTopBarImageBtnHeight = array .getDimensionPixelSize (R .styleable .QMUITopBar_qmui_topbar_image_btn_height , QMUIDisplayHelper .dp2px (context , 48 ));
143+ mTopBarTextBtnPaddingHor = array .getDimensionPixelSize (R .styleable .QMUITopBar_qmui_topbar_text_btn_padding_horizontal , QMUIDisplayHelper .dp2px (context , 12 ));
144+ mTopBarTextBtnTextColor = array .getColorStateList (R .styleable .QMUITopBar_qmui_topbar_text_btn_color_state_list );
145+ mTopBarTextBtnTextSize = array .getDimensionPixelSize (R .styleable .QMUITopBar_qmui_topbar_text_btn_text_size , QMUIDisplayHelper .sp2px (context , 16 ));
146+
147+ }
148+
124149 @ Override
125150 protected void onAttachedToWindow () {
126151 super .onAttachedToWindow ();
@@ -211,7 +236,7 @@ private TextView getTitleView(boolean isEmoji) {
211236 mTitleView .setGravity (Gravity .CENTER );
212237 mTitleView .setSingleLine (true );
213238 mTitleView .setEllipsize (TruncateAt .MIDDLE );
214- mTitleView .setTextColor (QMUIResHelper . getAttrColor ( getContext (), R . attr . qmui_topbar_title_color ) );
239+ mTitleView .setTextColor (mTitleTextColor );
215240 updateTitleViewStyle ();
216241 LinearLayout .LayoutParams titleLp = generateTitleViewAndSubTitleViewLp ();
217242 makeSureTitleContainerView ().addView (mTitleView , titleLp );
@@ -226,9 +251,9 @@ private TextView getTitleView(boolean isEmoji) {
226251 private void updateTitleViewStyle () {
227252 if (mTitleView != null ) {
228253 if (mSubTitleView == null || QMUILangHelper .isNullOrEmpty (mSubTitleView .getText ())) {
229- mTitleView .setTextSize (TypedValue .COMPLEX_UNIT_PX , QMUIResHelper . getAttrDimen ( getContext (), R . attr . qmui_topbar_title_text_size ) );
254+ mTitleView .setTextSize (TypedValue .COMPLEX_UNIT_PX , mTitleTextSize );
230255 } else {
231- mTitleView .setTextSize (TypedValue .COMPLEX_UNIT_PX , QMUIResHelper . getAttrDimen ( getContext (), R . attr . qmui_topbar_title_text_size_with_subtitle ) );
256+ mTitleView .setTextSize (TypedValue .COMPLEX_UNIT_PX , mTitleTextSizeWithSubTitle );
232257 }
233258 }
234259 }
@@ -265,8 +290,8 @@ private TextView getSubTitleView() {
265290 mSubTitleView .setGravity (Gravity .CENTER );
266291 mSubTitleView .setSingleLine (true );
267292 mSubTitleView .setEllipsize (TruncateAt .MIDDLE );
268- mSubTitleView .setTextSize (TypedValue .COMPLEX_UNIT_PX , QMUIResHelper . getAttrDimen ( getContext (), R . attr . qmui_topbar_subtitle_text_size ) );
269- mSubTitleView .setTextColor (QMUIResHelper . getAttrColor ( getContext (), R . attr . qmui_topbar_subtitle_color ) );
293+ mSubTitleView .setTextSize (TypedValue .COMPLEX_UNIT_PX , mSubTitleTextSize );
294+ mSubTitleView .setTextColor (mSubTitleTextColor );
270295 LinearLayout .LayoutParams titleLp = generateTitleViewAndSubTitleViewLp ();
271296 titleLp .topMargin = QMUIDisplayHelper .dp2px (getContext (), 1 );
272297 makeSureTitleContainerView ().addView (mSubTitleView , titleLp );
@@ -315,8 +340,7 @@ private LinearLayout makeSureTitleContainerView() {
315340 // 垂直,后面要支持水平的话可以加个接口来设置
316341 mTitleContainerView .setOrientation (LinearLayout .VERTICAL );
317342 mTitleContainerView .setGravity (Gravity .CENTER );
318- int horPadding = QMUIResHelper .getAttrDimen (getContext (), R .attr .qmui_topbar_title_container_padding_horizontal );
319- mTitleContainerView .setPadding (horPadding , 0 , horPadding , 0 );
343+ mTitleContainerView .setPadding (mTitleContainerPaddingHor , 0 , mTitleContainerPaddingHor , 0 );
320344 addView (mTitleContainerView , generateTitleContainerViewLp ());
321345 }
322346 return mTitleContainerView ;
@@ -420,8 +444,8 @@ public void addRightView(View view, int viewId, LayoutParams layoutParams) {
420444 * 生成一个 LayoutParams,当把 Button addView 到 TopBar 时,使用这个 LayouyParams
421445 */
422446 public LayoutParams generateTopBarImageButtonLayoutParams () {
423- LayoutParams lp = new LayoutParams (getTopBarImageBtnWidth (), getTopBarImageBtnHeight () );
424- lp .topMargin = Math .max (0 , (getTopBarHeight () - getTopBarImageBtnHeight () ) / 2 );
447+ LayoutParams lp = new LayoutParams (mTopBarImageBtnWidth , mTopBarImageBtnHeight );
448+ lp .topMargin = Math .max (0 , (getTopBarHeight () - mTopBarImageBtnHeight ) / 2 );
425449 return lp ;
426450 }
427451
@@ -455,8 +479,8 @@ public QMUIAlphaImageButton addLeftImageButton(int drawableResId, int viewId) {
455479 * 生成一个LayoutParams,当把 Button addView 到 TopBar 时,使用这个 LayouyParams
456480 */
457481 public LayoutParams generateTopBarTextButtonLayoutParams () {
458- LayoutParams lp = new LayoutParams (LayoutParams .WRAP_CONTENT , getTopBarImageBtnHeight () );
459- lp .topMargin = Math .max (0 , (getTopBarHeight () - getTopBarImageBtnHeight () ) / 2 );
482+ LayoutParams lp = new LayoutParams (LayoutParams .WRAP_CONTENT , mTopBarImageBtnHeight );
483+ lp .topMargin = Math .max (0 , (getTopBarHeight () - mTopBarImageBtnHeight ) / 2 );
460484 return lp ;
461485 }
462486
@@ -521,10 +545,9 @@ private Button generateTopBarTextButton(String text) {
521545 button .setMinHeight (0 );
522546 button .setMinimumWidth (0 );
523547 button .setMinimumHeight (0 );
524- int paddingHorizontal = getTopBarTextBtnPaddingHorizontal ();
525- button .setPadding (paddingHorizontal , 0 , paddingHorizontal , 0 );
526- button .setTextColor (QMUIResHelper .getAttrColorStateList (getContext (), R .attr .qmui_topbar_text_btn_color_state_list ));
527- button .setTextSize (TypedValue .COMPLEX_UNIT_PX , QMUIResHelper .getAttrDimen (getContext (), R .attr .qmui_topbar_text_btn_text_size ));
548+ button .setPadding (mTopBarTextBtnPaddingHor , 0 , mTopBarTextBtnPaddingHor , 0 );
549+ button .setTextColor (mTopBarTextBtnTextColor );
550+ button .setTextSize (TypedValue .COMPLEX_UNIT_PX , mTopBarTextBtnTextSize );
528551 button .setGravity (Gravity .CENTER );
529552 button .setText (text );
530553 return button ;
@@ -599,27 +622,6 @@ private int getTopBarHeight() {
599622 return mTopbarHeight ;
600623 }
601624
602- protected int getTopBarImageBtnWidth () {
603- if (mTopbarImageBtnWidth == -1 ) {
604- mTopbarImageBtnWidth = QMUIResHelper .getAttrDimen (getContext (), R .attr .qmui_topbar_image_btn_height );
605- }
606- return mTopbarImageBtnWidth ;
607- }
608-
609- protected int getTopBarImageBtnHeight () {
610- if (mTopbarImageBtnHeight == -1 ) {
611- mTopbarImageBtnHeight = QMUIResHelper .getAttrDimen (getContext (), R .attr .qmui_topbar_image_btn_height );
612- }
613- return mTopbarImageBtnHeight ;
614- }
615-
616- private int getTopBarTextBtnPaddingHorizontal () {
617- if (mTopbarTextBtnPaddingHorizontal == -1 ) {
618- mTopbarTextBtnPaddingHorizontal = QMUIResHelper .getAttrDimen (getContext (), R .attr .qmui_topbar_text_btn_padding_horizontal );
619- }
620- return mTopbarTextBtnPaddingHorizontal ;
621- }
622-
623625 // ======================== TopBar自身相关的方法
624626
625627 /**
@@ -686,23 +688,19 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
686688 if ((mTitleGravity & Gravity .HORIZONTAL_GRAVITY_MASK ) == Gravity .CENTER_HORIZONTAL ) {
687689 if (leftViewWidth == 0 && rightViewWidth == 0 ) {
688690 // 左右没有按钮时,title 距离 TopBar 左右边缘的距离
689- int titleMarginHorizontalWithoutButton = QMUIResHelper .getAttrDimen (getContext (),
690- R .attr .qmui_topbar_title_margin_horizontal_when_no_btn_aside );
691- leftViewWidth += titleMarginHorizontalWithoutButton ;
692- rightViewWidth += titleMarginHorizontalWithoutButton ;
691+ leftViewWidth += mTitleMarginHorWhenNoBtnAside ;
692+ rightViewWidth += mTitleMarginHorWhenNoBtnAside ;
693693 }
694694
695695 // 标题水平居中,左右两侧的占位要保持一致
696696 titleContainerWidth = MeasureSpec .getSize (widthMeasureSpec ) - Math .max (leftViewWidth , rightViewWidth ) * 2 - getPaddingLeft () - getPaddingRight ();
697697 } else {
698698 // 标题非水平居中,左右没有按钮时,间距分别计算
699699 if (leftViewWidth == 0 ) {
700- leftViewWidth += QMUIResHelper .getAttrDimen (getContext (),
701- R .attr .qmui_topbar_title_margin_horizontal_when_no_btn_aside );
700+ leftViewWidth += mTitleMarginHorWhenNoBtnAside ;
702701 }
703702 if (rightViewWidth == 0 ) {
704- rightViewWidth += QMUIResHelper .getAttrDimen (getContext (),
705- R .attr .qmui_topbar_title_margin_horizontal_when_no_btn_aside );
703+ rightViewWidth += mTitleMarginHorWhenNoBtnAside ;
706704 }
707705
708706 // 标题非水平居中,左右两侧的占位按实际计算即可
0 commit comments