@@ -86,13 +86,15 @@ public interface IconTabProvider {
86
86
private int indicatorColor = 0xffFF811C ; // 指示器颜色
87
87
private int underlineColor = 0xffCECECE ; // 下划线颜色
88
88
private int dividerColor = 0xFF811C ; // 分割线颜色
89
- private int indicatorPadding = 10 ; //指示器 左右内边距
89
+ private float indicatorPadding = 10 ; //指示器 左右内边距
90
90
91
91
private boolean shouldExpand = false ;//FF811C
92
92
private boolean textAllCaps = true ;
93
+ private boolean isFixedIndicatorWidth = false ;
93
94
94
95
private int scrollOffset = 52 ;
95
96
private int indicatorHeight = 0 ; // 指示器高度
97
+ private int indicatorWidth = 0 ; // 指示器宽度
96
98
private int underlineHeight = 0 ; // 下划线高度
97
99
private int dividerPadding = 6 ; // 分割线
98
100
private int tabPadding = 22 ;
@@ -140,15 +142,14 @@ public PagerSlidingTabStrip(Context context, AttributeSet attrs,
140
142
TypedValue .COMPLEX_UNIT_DIP , scrollOffset , dm );
141
143
indicatorHeight = (int ) TypedValue .applyDimension (
142
144
TypedValue .COMPLEX_UNIT_DIP , indicatorHeight , dm );
145
+ indicatorWidth = (int ) TypedValue .applyDimension (TypedValue .COMPLEX_UNIT_DIP , indicatorWidth , dm );
143
146
underlineHeight = (int ) TypedValue .applyDimension (
144
147
TypedValue .COMPLEX_UNIT_DIP , underlineHeight , dm );
145
148
dividerPadding = (int ) TypedValue .applyDimension (
146
149
TypedValue .COMPLEX_UNIT_DIP , dividerPadding , dm );
147
- tabPadding = (int ) TypedValue .applyDimension (
148
- TypedValue .COMPLEX_UNIT_DIP , tabPadding , dm );
149
150
tabPaddingr = (int ) TypedValue .applyDimension (
150
151
TypedValue .COMPLEX_UNIT_DIP , tabPaddingr , dm );
151
- indicatorPadding = ( int ) TypedValue .applyDimension (
152
+ indicatorPadding = TypedValue .applyDimension (
152
153
TypedValue .COMPLEX_UNIT_DIP , indicatorPadding , dm );
153
154
dividerWidth = (int ) TypedValue .applyDimension (
154
155
TypedValue .COMPLEX_UNIT_DIP , dividerWidth , dm );
@@ -170,15 +171,17 @@ public PagerSlidingTabStrip(Context context, AttributeSet attrs,
170
171
a = context .obtainStyledAttributes (attrs , R .styleable .PagerSlidingTabStrip );
171
172
172
173
indicatorColor = a .getColor (R .styleable .PagerSlidingTabStrip_indicatorColor , indicatorColor );
173
- indicatorPadding = a .getDimensionPixelSize (R .styleable .PagerSlidingTabStrip_indicatorPaddingLeftRight , indicatorPadding );
174
+ indicatorPadding = a .getDimensionPixelSize (R .styleable .PagerSlidingTabStrip_indicatorPaddingLeftRight , ( int ) indicatorPadding );
174
175
underlineColor = a .getColor (R .styleable .PagerSlidingTabStrip_underlineColor , underlineColor );
175
176
dividerColor = a .getColor (R .styleable .PagerSlidingTabStrip_dividerColor , dividerColor );
176
177
indicatorHeight = a .getDimensionPixelSize (R .styleable .PagerSlidingTabStrip_indicatorHeight , indicatorHeight );
178
+ indicatorWidth = a .getDimensionPixelSize (R .styleable .PagerSlidingTabStrip_indicatorWidth , indicatorWidth );
177
179
underlineHeight = a .getDimensionPixelSize (R .styleable .PagerSlidingTabStrip_underlineHeight , underlineHeight );
178
180
dividerPadding = a .getDimensionPixelSize (R .styleable .PagerSlidingTabStrip_dividerPadding , dividerPadding );
179
181
tabPadding = a .getDimensionPixelSize (R .styleable .PagerSlidingTabStrip_tabPaddingLeftRight , tabPadding );
180
182
tabBackgroundResId = a .getResourceId (R .styleable .PagerSlidingTabStrip_tabBackground , tabBackgroundResId );
181
183
shouldExpand = a .getBoolean (R .styleable .PagerSlidingTabStrip_shouldExpand , shouldExpand );
184
+ isFixedIndicatorWidth = a .getBoolean (R .styleable .PagerSlidingTabStrip_isFixedIndicatorWidth , isFixedIndicatorWidth );
182
185
scrollOffset = a .getDimensionPixelSize (R .styleable .PagerSlidingTabStrip_scrollOffset , scrollOffset );
183
186
// textAllCaps = a.getBoolean(R.styleable.PagerSlidingTabStrip_textAllCaps, textAllCaps);
184
187
tabTextSize = a .getDimensionPixelSize (R .styleable .PagerSlidingTabStrip_tabDefTextSize , tabTextSize );
@@ -316,30 +319,18 @@ private void updateTabStyles() {
316
319
317
320
View v = tabsContainer .getChildAt (i );
318
321
319
- v .setLayoutParams (defaultTabLayoutParams );
320
322
v .setBackgroundResource (tabBackgroundResId );
321
323
322
324
if (v instanceof TextView ) {
323
325
324
326
TextView tab = (TextView ) v ;
325
327
tab .setTypeface (tabTypeface , tabTypefaceStyle );
326
- v .setPadding (tabPadding , 0 , tabPadding , 0 );
327
328
if (currentIndex == i ) {
328
329
tab .setTextSize (TypedValue .COMPLEX_UNIT_PX , tabTextSizer );
329
330
tab .setTextColor (tabTextColorr );
330
- // if (shouldExpand) {
331
- // v.setPadding(tabPadding, 0, tabPadding, 0);
332
- // } else {
333
- // v.setPadding(tabPadding, 0, tabPadding, 0);
334
- // }
335
331
} else {
336
332
tab .setTextSize (TypedValue .COMPLEX_UNIT_PX , tabTextSize );
337
333
tab .setTextColor (tabTextColor );
338
- // if (shouldExpand) {
339
- // v.setPadding(0, 0, 0, 0);
340
- // } else {
341
- // v.setPadding(0, 0, 0, 0);
342
- // }
343
334
}
344
335
// setAllCaps() is only available from API 14, so the upper case
345
336
// is made manually if we are on a
@@ -361,24 +352,27 @@ private void updateTabStyles() {
361
352
protected void onMeasure (int widthMeasureSpec , int heightMeasureSpec ) {
362
353
super .onMeasure (widthMeasureSpec , heightMeasureSpec );
363
354
364
- if (!shouldExpand
365
- || MeasureSpec .getMode (widthMeasureSpec ) == MeasureSpec .UNSPECIFIED ) {
366
- return ;
367
- }
355
+ // if (!shouldExpand
356
+ // || MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED) {
357
+ // return;
358
+ // }
368
359
369
360
int myWidth = getMeasuredWidth ();
370
361
int childWidth = 0 ;
371
362
for (int i = 0 ; i < pageCount ; i ++) {
372
- childWidth += tabsContainer .getChildAt (i ).getMeasuredWidth ();
363
+ childWidth += tabsContainer .getChildAt (i ).getMeasuredWidth () + ( tabPadding * 2 ) ;
373
364
}
374
365
375
366
if (!checkedTabWidths && childWidth > 0 && myWidth > 0 ) {
376
-
377
- if (childWidth <= myWidth ) {
378
- for (int i = 0 ; i < pageCount ; i ++) {
379
- tabsContainer .getChildAt (i ).setLayoutParams (
380
- expandedTabLayoutParams );
381
- }
367
+ LinearLayout .LayoutParams lp ;
368
+ if (childWidth > myWidth ) {
369
+ lp = defaultTabLayoutParams ;
370
+ } else {
371
+ // if (myWidth / pageCount < maxItemWidth) expandedTabLayoutParamsWrap else
372
+ lp = expandedTabLayoutParams ;
373
+ }
374
+ for (int i = 0 ; i < pageCount ; i ++) {
375
+ tabsContainer .getChildAt (i ).setLayoutParams (lp );
382
376
}
383
377
384
378
checkedTabWidths = true ;
@@ -436,7 +430,9 @@ protected void onDraw(Canvas canvas) {
436
430
lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset )
437
431
* lineRight );
438
432
}
439
-
433
+ if (isFixedIndicatorWidth ) {
434
+ indicatorPadding = (lineRight - lineLeft - indicatorWidth ) / 2 ;
435
+ }
440
436
canvas .drawRect (lineLeft + indicatorPadding , height - indicatorHeight , lineRight - indicatorPadding , height ,
441
437
rectPaint );
442
438
0 commit comments