44import android .content .res .TypedArray ;
55import android .graphics .Canvas ;
66import android .graphics .Color ;
7- import android .text .TextUtils ;
87import android .util .AttributeSet ;
9- import android .view .Gravity ;
10- import android .view .View ;
11- import android .widget .ImageView ;
12- import android .widget .RelativeLayout ;
13- import android .widget .TextView ;
8+ import android .view .LayoutInflater ;
9+ import android .widget .LinearLayout ;
1410
1511import com .pengxh .androidx .lite .R ;
16- import com .pengxh .androidx .lite .hub .ContextHub ;
17- import com .pengxh .androidx .lite .hub .IntHub ;
12+ import com .pengxh .androidx .lite .databinding .WidgetViewTitleBarBinding ;
1813
1914/**
2015 * 界面顶部标题栏
2116 */
22- public class TitleBarView extends RelativeLayout {
17+ public class TitleBarView extends LinearLayout {
2318
24- private final int titleHeight = IntHub .dp2px (getContext (), 45 );
25- private final TextView textView ;
19+ private final WidgetViewTitleBarBinding binding ;
2620
2721 public TitleBarView (Context context , AttributeSet attrs ) {
2822 super (context , attrs );
23+ binding = WidgetViewTitleBarBinding .inflate (LayoutInflater .from (context ), this , true );
24+
2925 TypedArray type = context .obtainStyledAttributes (attrs , R .styleable .TitleBarView );
3026 int leftImageRes = type .getResourceId (R .styleable .TitleBarView_tbv_left_image , R .drawable .ic_title_left );
3127 boolean isShowLeft = type .getBoolean (R .styleable .TitleBarView_tbv_show_left_image , true );
3228 int rightImageRes = type .getResourceId (R .styleable .TitleBarView_tbv_right_image , R .drawable .ic_title_right );
3329 boolean isShowRight = type .getBoolean (R .styleable .TitleBarView_tbv_show_right_image , false );
3430 CharSequence title = type .getText (R .styleable .TitleBarView_tbv_text );
3531 int titleColor = type .getColor (R .styleable .TitleBarView_tbv_text_color , Color .WHITE );
36- float titleSize = type .getDimension (R .styleable .TitleBarView_tbv_text_size , 18f );
37- boolean onlyShowTitle = type .getBoolean (R .styleable .TitleBarView_tbv_only_show_title , false );
32+ boolean isSmallerTitle = type .getBoolean (R .styleable .TitleBarView_tbv_smaller_title , false );
3833 type .recycle ();
3934
40- if (onlyShowTitle ) {
41- //文字
42- RelativeLayout .LayoutParams titleParams = new RelativeLayout .LayoutParams (LayoutParams .WRAP_CONTENT , LayoutParams .WRAP_CONTENT );
43- titleParams .height = titleHeight ;
44- textView = new TextView (context );
45- textView .setText (title );
46- textView .setSingleLine (true );
47- textView .setEllipsize (TextUtils .TruncateAt .END );
48- //textSize会将值默认为sp,不除以像素密度则会将sp再此转为px,相当于原本字体大小进行了两次转换px
49- textView .setTextSize (titleSize / ContextHub .getScreenDensity (context ));
50- textView .setGravity (Gravity .CENTER );
51- textView .setTextColor (titleColor );
52- titleParams .addRule (CENTER_IN_PARENT , TRUE );
53- textView .setLayoutParams (titleParams );
54- addView (textView );
55- } else {
56- int iconSize = IntHub .dp2px (getContext (), 25 );
57- int textMargin = IntHub .dp2px (getContext (), 10 );
58-
59- //左边图标
60- if (isShowLeft ) {
61- RelativeLayout .LayoutParams leftImageParams = new RelativeLayout .LayoutParams (iconSize , iconSize );
62- ImageView leftImageView = new ImageView (context );
63- leftImageView .setImageResource (leftImageRes );
64- leftImageView .setScaleType (ImageView .ScaleType .CENTER_INSIDE );
65- leftImageParams .setMarginStart (textMargin );
66- leftImageParams .addRule (CENTER_VERTICAL , TRUE );
67- addView (leftImageView , leftImageParams );
68- leftImageView .setOnClickListener (new View .OnClickListener () {
69- @ Override
70- public void onClick (View v ) {
71- try {
72- listener .onLeftClick ();
73- } catch (NullPointerException e ) {
74- e .printStackTrace ();
75- }
76- }
77- });
78- }
79-
80- //文字
81- RelativeLayout .LayoutParams titleParams = new RelativeLayout .LayoutParams (LayoutParams .WRAP_CONTENT , LayoutParams .WRAP_CONTENT );
82- titleParams .height = titleHeight ;
83- textView = new TextView (context );
84- textView .setText (title );
85- textView .setSingleLine (true );
86- textView .setEllipsize (TextUtils .TruncateAt .END );
87- //textSize会将值默认为sp,不除以像素密度则会将sp再此转为px,相当于原本字体大小进行了两次转换px
88- textView .setTextSize (titleSize / ContextHub .getScreenDensity (context ));
89- textView .setGravity (Gravity .CENTER );
90- textView .setTextColor (titleColor );
91- titleParams .addRule (CENTER_IN_PARENT , TRUE );
92- textView .setLayoutParams (titleParams );
93- addView (textView );
94-
95- //右边图标
96- if (isShowRight ) {
97- RelativeLayout .LayoutParams rightImageParams = new RelativeLayout .LayoutParams (iconSize , iconSize );
98- ImageView rightImageView = new ImageView (context );
99- rightImageView .setImageResource (rightImageRes );
100- rightImageView .setScaleType (ImageView .ScaleType .CENTER_INSIDE );
101- rightImageParams .setMarginEnd (textMargin );
102- rightImageParams .addRule (CENTER_VERTICAL , TRUE );
103- rightImageParams .addRule (ALIGN_PARENT_END , TRUE );
104- addView (rightImageView , rightImageParams );
105- rightImageView .setOnClickListener (new View .OnClickListener () {
106- @ Override
107- public void onClick (View v ) {
108- try {
109- listener .onRightClick ();
110- } catch (NullPointerException e ) {
111- e .printStackTrace ();
112- }
113- }
114- });
115- }
35+ //左边图标
36+ if (isShowLeft ) {
37+ binding .leftButton .setImageResource (leftImageRes );
38+ binding .leftButton .setOnClickListener (v -> {
39+ if (listener == null ) {
40+ throw new NullPointerException ("listener is null" );
41+ }
42+ listener .onLeftClick ();
43+ });
11644 }
117- }
11845
119- @ Override
120- protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
121- super .onMeasure (widthMeasureSpec , heightMeasureSpec );
122- int widthSpecSize = MeasureSpec .getSize (widthMeasureSpec );
123- setMeasuredDimension (widthSpecSize , titleHeight );
46+ //文字
47+ binding .titleView .setText (title );
48+ float textSize ;
49+ if (isSmallerTitle ) {
50+ textSize = 16f ;
51+ } else {
52+ textSize = 18f ;
53+ }
54+ binding .titleView .setTextSize (textSize );
55+ binding .titleView .setTextColor (titleColor );
56+
57+ //右边图标
58+ if (isShowRight ) {
59+ binding .rightButton .setImageResource (rightImageRes );
60+ binding .rightButton .setOnClickListener (v -> {
61+ if (listener == null ) {
62+ throw new NullPointerException ("listener is null" );
63+ }
64+ listener .onRightClick ();
65+ });
66+ }
12467 }
12568
12669 @ Override
@@ -134,15 +77,15 @@ protected void onDraw(Canvas canvas) {
13477 * 动态设置标题
13578 */
13679 public void setTitle (String title ) {
137- textView .setText (title );
80+ binding . titleView .setText (title );
13881 invalidate ();
13982 }
14083
14184 /**
14285 * 获取当前显示标题文字
14386 */
14487 public String getTitle () {
145- return textView .getText ().toString ();
88+ return binding . titleView .getText ().toString ();
14689 }
14790
14891 private OnClickListener listener ;
0 commit comments