9
9
import android .util .AttributeSet ;
10
10
import android .util .TypedValue ;
11
11
import android .view .View ;
12
+ import android .view .ViewGroup ;
12
13
import android .widget .FrameLayout ;
13
14
import android .widget .LinearLayout ;
14
15
import android .widget .TextView ;
@@ -70,12 +71,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
70
71
heightMeasureSpec = MeasureSpec .makeMeasureSpec (titleBarHeight , MeasureSpec .EXACTLY );
71
72
}
72
73
73
- Drawable background = getBackground ();
74
+ final Drawable background = getBackground ();
74
75
// 如果当前背景是一张图片的话
75
76
if (background instanceof BitmapDrawable ) {
76
77
mMainLayout .getLayoutParams ().height = MeasureSpec .getSize (heightMeasureSpec );
77
78
// 算出标题栏的宽度和图片的宽度之比例
78
- double ratio = (double ) MeasureSpec .getSize (widthMeasureSpec ) / (double ) background .getIntrinsicWidth ();
79
+ final double ratio = (double ) MeasureSpec .getSize (widthMeasureSpec ) / (double ) background .getIntrinsicWidth ();
79
80
heightMeasureSpec = MeasureSpec .makeMeasureSpec ((int ) (ratio * background .getIntrinsicHeight ()), MeasureSpec .EXACTLY );
80
81
}
81
82
}
@@ -141,18 +142,17 @@ private void initStyle(AttributeSet attrs) {
141
142
}
142
143
}
143
144
145
+ // 图标设置
144
146
if (ta .hasValue (R .styleable .TitleBar_title_right )) {
145
147
setRightTitle (ta .getString (R .styleable .TitleBar_title_right ));
146
148
}
147
149
148
150
if (ta .hasValue (R .styleable .TitleBar_icon_left )) {
149
151
setLeftIcon (getContext ().getResources ().getDrawable (ta .getResourceId (R .styleable .TitleBar_icon_left , 0 )));
150
152
}else {
151
- if (ta .getBoolean (R .styleable .TitleBar_icon_back , true )) {
153
+ if (ta .getBoolean (R .styleable .TitleBar_icon_back , style . getBackIconResource () > 0 )) {
152
154
// 显示返回图标
153
- if (style .getBackIconResource () != 0 ) {
154
- setLeftIcon (getContext ().getResources ().getDrawable (style .getBackIconResource ()));
155
- }
155
+ setLeftIcon (getContext ().getResources ().getDrawable (style .getBackIconResource ()));
156
156
}
157
157
}
158
158
@@ -171,16 +171,13 @@ private void initStyle(AttributeSet attrs) {
171
171
setRightSize (TypedValue .COMPLEX_UNIT_PX , ta .getDimensionPixelSize (R .styleable .TitleBar_size_right , ViewBuilder .sp2px (getContext (), style .getRightViewSize ())));
172
172
173
173
// 背景设置
174
- if (style .getLeftViewBackground () != 0 ) {
175
- setLeftBackground (ta .getResourceId (R .styleable .TitleBar_background_left , style .getLeftViewBackground ()));
176
- }
177
- if (style .getRightViewBackground () != 0 ) {
178
- setRightBackground (ta .getResourceId (R .styleable .TitleBar_background_right , style .getRightViewBackground ()));
179
- }
174
+ setLeftBackground (ta .getResourceId (R .styleable .TitleBar_background_left , style .getLeftViewBackground ()));
175
+ setRightBackground (ta .getResourceId (R .styleable .TitleBar_background_right , style .getRightViewBackground ()));
180
176
181
177
// 分割线设置
182
- setLineVisible (ta .getBoolean (R .styleable .TitleBar_line , style .getLineVisibility ()));
183
- setLineColor (ta .getColor (R .styleable .TitleBar_color_line , style .getLineBackgroundColor ()));
178
+ setLineVisible (ta .getBoolean (R .styleable .TitleBar_line_visible , style .isLineVisible ()));
179
+ setLineColor (ta .getColor (R .styleable .TitleBar_line_color , style .getLineColor ()));
180
+ setLineSize (ta .getDimensionPixelSize (R .styleable .TitleBar_line_size , style .getLineSize ()));
184
181
185
182
// 回收TypedArray
186
183
ta .recycle ();
@@ -219,6 +216,23 @@ public void run() {
219
216
}
220
217
}
221
218
219
+ /**
220
+ * {@link View.OnClickListener}
221
+ */
222
+ @ Override
223
+ public void onClick (View v ) {
224
+ if (getOnTitleBarListener () == null ) return ;
225
+
226
+ final int id = v .getId ();
227
+ if (id == R .id .bar_id_left_view ) {
228
+ getOnTitleBarListener ().onLeftClick (v );
229
+ }else if (id == R .id .bar_id_title_view ) {
230
+ getOnTitleBarListener ().onTitleClick (v );
231
+ }else if (id == R .id .bar_id_right_view ) {
232
+ getOnTitleBarListener ().onRightClick (v );
233
+ }
234
+ }
235
+
222
236
@ Override
223
237
protected void onAttachedToWindow () {
224
238
super .onAttachedToWindow ();
@@ -237,23 +251,6 @@ protected void onDetachedFromWindow() {
237
251
super .onDetachedFromWindow ();
238
252
}
239
253
240
- /**
241
- * {@link View.OnClickListener}
242
- */
243
- @ Override
244
- public void onClick (View v ) {
245
- if (getOnTitleBarListener () == null ) return ;
246
-
247
- final int id = v .getId ();
248
- if (id == R .id .bar_id_left_view ) {
249
- getOnTitleBarListener ().onLeftClick (v );
250
- }else if (id == R .id .bar_id_title_view ) {
251
- getOnTitleBarListener ().onTitleClick (v );
252
- }else if (id == R .id .bar_id_right_view ) {
253
- getOnTitleBarListener ().onRightClick (v );
254
- }
255
- }
256
-
257
254
/**
258
255
* 获取点击监听器
259
256
*/
@@ -278,61 +275,65 @@ public CharSequence getTitle() {
278
275
/**
279
276
* 设置标题
280
277
*/
281
- public void setTitle (int resourceId ) {
282
- setTitle (getResources ().getString (resourceId ));
278
+ public void setTitle (int stringId ) {
279
+ setTitle (getResources ().getString (stringId ));
283
280
}
284
281
285
282
public void setTitle (CharSequence text ) {
286
283
mTitleView .setText (text );
287
- postDelayed (this , 30 );
284
+ post (this );
288
285
}
289
286
290
287
/**
291
288
* 设置左边的标题
292
289
*/
293
- public void setLeftTitle (int resourceId ) {
294
- setLeftTitle (getResources ().getString (resourceId ));
290
+ public void setLeftTitle (int stringId ) {
291
+ setLeftTitle (getResources ().getString (stringId ));
295
292
}
296
293
297
294
public void setLeftTitle (CharSequence text ) {
298
295
mLeftView .setText (text );
299
- postDelayed (this , 30 );
296
+ post (this );
300
297
}
301
298
302
299
/**
303
300
* 设置右边的标题
304
301
*/
305
- public void setRightTitle (int resourceId ) {
306
- setRightTitle (getResources ().getString (resourceId ));
302
+ public void setRightTitle (int stringId ) {
303
+ setRightTitle (getResources ().getString (stringId ));
307
304
}
308
305
309
306
public void setRightTitle (CharSequence text ) {
310
307
mRightView .setText (text );
311
- postDelayed (this , 30 );
308
+ post (this );
312
309
}
313
310
314
311
/**
315
312
* 设置左边的图标
316
313
*/
317
- public void setLeftIcon (int resourceId ) {
318
- setLeftIcon (getContext ().getResources ().getDrawable (resourceId ));
314
+ public void setLeftIcon (int iconId ) {
315
+ if (iconId > 0 ) {
316
+ setLeftIcon (getContext ().getResources ().getDrawable (iconId ));
317
+ }
319
318
}
320
319
321
320
public void setLeftIcon (Drawable drawable ) {
322
321
mLeftView .setCompoundDrawablesWithIntrinsicBounds (drawable , null , null , null );
323
- postDelayed (this , 30 );
322
+ post (this );
324
323
}
325
324
326
325
/**
327
326
* 设置右边的图标
328
327
*/
329
- public void setRightIcon (int resourceId ) {
330
- setRightIcon (getContext ().getResources ().getDrawable (resourceId ));
328
+ public void setRightIcon (int iconId ) {
329
+ if (iconId > 0 ) {
330
+ setRightIcon (getContext ().getResources ().getDrawable (iconId ));
331
+ }
331
332
}
332
333
333
334
public void setRightIcon (Drawable drawable ) {
334
335
mRightView .setCompoundDrawablesWithIntrinsicBounds (null , null , drawable , null );
335
- postDelayed (this , 30 );
336
+ post (this );
336
337
}
337
338
338
339
/**
@@ -359,8 +360,10 @@ public void setRightColor(int color) {
359
360
/**
360
361
* 设置左标题状态选择器
361
362
*/
362
- public void setLeftBackground (int resourceId ) {
363
- mLeftView .setBackgroundResource (resourceId );
363
+ public void setLeftBackground (int bgId ) {
364
+ if (bgId > 0 ) {
365
+ mLeftView .setBackgroundResource (bgId );
366
+ }
364
367
}
365
368
366
369
public void setLeftBackground (Drawable drawable ) {
@@ -374,8 +377,10 @@ public void setLeftBackground(Drawable drawable) {
374
377
/**
375
378
* 设置右标题状态选择器
376
379
*/
377
- public void setRightBackground (int resourceId ) {
378
- mRightView .setBackgroundResource (resourceId );
380
+ public void setRightBackground (int bgId ) {
381
+ if (bgId > 0 ) {
382
+ mRightView .setBackgroundResource (bgId );
383
+ }
379
384
}
380
385
381
386
public void setRightBackground (Drawable drawable ) {
@@ -421,6 +426,15 @@ public void setLineColor(int color) {
421
426
mLineView .setBackgroundColor (color );
422
427
}
423
428
429
+ /**
430
+ * 设置分割线的大小
431
+ */
432
+ public void setLineSize (int size ) {
433
+ ViewGroup .LayoutParams layoutParams = mLineView .getLayoutParams ();
434
+ layoutParams .height = size ;
435
+ mLineView .setLayoutParams (layoutParams );
436
+ }
437
+
424
438
/**
425
439
* 获取主要的布局对象
426
440
*/
0 commit comments