Skip to content

Commit 0d333e5

Browse files
author
Faqiang Lv
committed
updateCode
1 parent 4894a11 commit 0d333e5

File tree

4 files changed

+680
-29
lines changed

4 files changed

+680
-29
lines changed

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,45 @@
22
用于Android 日常代码测试,效果整理
33

44
### Update Log:
5+
- PagerSlidingTabStrip 整理(对之前整理的进一步的调整), [Kotlin 版本](https://github.com/lvfaqiang/AndroidTestCode/blob/master/app/src/main/java/com/lvfq/code/view/PagerSlidingTabStrip.kt)[Java版本](https://github.com/lvfaqiang/AndroidTestCode/blob/master/app/src/main/java/com/lvfq/code/tablayout/PagerSlidingTabStrip.java)
6+
```
7+
<declare-styleable name="PagerSlidingTabStrip">
8+
<!-- 指示器颜色 -->
9+
<attr name="indicatorColor" format="color" />
10+
<!-- 下划线颜色 -->
11+
<attr name="underlineColor" format="color" />
12+
<!-- 分割线颜色 -->
13+
<attr name="dividerColor" format="color" />
14+
<!-- 指示器高度 -->
15+
<attr name="indicatorHeight" format="dimension" />
16+
<!-- 指示器宽度 ,isFixedIndicatorWidth 为 true 时有效 -->
17+
<attr name="indicatorWidth" format="dimension" />
18+
<!-- 是否固定指示器宽度 -->
19+
<attr name="isFixedIndicatorWidth" format="boolean" />
20+
<!-- 指示器左右内边距 -->
21+
<attr name="indicatorPaddingLeftRight" format="dimension" />
22+
<!-- 下划线高度 -->
23+
<attr name="underlineHeight" format="dimension" />
24+
<!-- 分割线内边距 -->
25+
<attr name="dividerPadding" format="dimension" />
26+
<!-- 每个 Tab 的 左右内边距 -->
27+
<attr name="tabPaddingLeftRight" format="dimension" />
28+
<!-- 滚动偏移值 -->
29+
<attr name="scrollOffset" format="dimension" />
30+
<!-- tab 背景 -->
31+
<attr name="tabBackground" format="reference" />
32+
<!-- 暂时无用 -->
33+
<attr name="shouldExpand" format="boolean" />
34+
<!-- tab 选中文字大小 -->
35+
<attr name="tabSelTextSize" format="dimension" />
36+
<!-- tab 默认文字大小 -->
37+
<attr name="tabDefTextSize" format="dimension" />
38+
<!-- tab 选中文字颜色 -->
39+
<attr name="tabSelTextColor" format="color" />
40+
<!-- tab 默认文字颜色 -->
41+
<attr name="tabDefTextColor" format="color" />
42+
</declare-styleable>
43+
```
544
- [strings.xml 配置 brand、tag 等公共文本](https://github.com/lvfaqiang/AndroidTestCode/blob/master/app/src/main/res/values/strings.xml)
645
- 2018年09月02日
746

app/src/main/java/com/lvfq/code/tablayout/PagerSlidingTabStrip.java

+25-29
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,15 @@ public interface IconTabProvider {
8686
private int indicatorColor = 0xffFF811C; // 指示器颜色
8787
private int underlineColor = 0xffCECECE; // 下划线颜色
8888
private int dividerColor = 0xFF811C; // 分割线颜色
89-
private int indicatorPadding = 10; //指示器 左右内边距
89+
private float indicatorPadding = 10; //指示器 左右内边距
9090

9191
private boolean shouldExpand = false;//FF811C
9292
private boolean textAllCaps = true;
93+
private boolean isFixedIndicatorWidth = false;
9394

9495
private int scrollOffset = 52;
9596
private int indicatorHeight = 0; // 指示器高度
97+
private int indicatorWidth = 0; // 指示器宽度
9698
private int underlineHeight = 0; // 下划线高度
9799
private int dividerPadding = 6; // 分割线
98100
private int tabPadding = 22;
@@ -140,15 +142,14 @@ public PagerSlidingTabStrip(Context context, AttributeSet attrs,
140142
TypedValue.COMPLEX_UNIT_DIP, scrollOffset, dm);
141143
indicatorHeight = (int) TypedValue.applyDimension(
142144
TypedValue.COMPLEX_UNIT_DIP, indicatorHeight, dm);
145+
indicatorWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, indicatorWidth, dm);
143146
underlineHeight = (int) TypedValue.applyDimension(
144147
TypedValue.COMPLEX_UNIT_DIP, underlineHeight, dm);
145148
dividerPadding = (int) TypedValue.applyDimension(
146149
TypedValue.COMPLEX_UNIT_DIP, dividerPadding, dm);
147-
tabPadding = (int) TypedValue.applyDimension(
148-
TypedValue.COMPLEX_UNIT_DIP, tabPadding, dm);
149150
tabPaddingr = (int) TypedValue.applyDimension(
150151
TypedValue.COMPLEX_UNIT_DIP, tabPaddingr, dm);
151-
indicatorPadding = (int) TypedValue.applyDimension(
152+
indicatorPadding = TypedValue.applyDimension(
152153
TypedValue.COMPLEX_UNIT_DIP, indicatorPadding, dm);
153154
dividerWidth = (int) TypedValue.applyDimension(
154155
TypedValue.COMPLEX_UNIT_DIP, dividerWidth, dm);
@@ -170,15 +171,17 @@ public PagerSlidingTabStrip(Context context, AttributeSet attrs,
170171
a = context.obtainStyledAttributes(attrs, R.styleable.PagerSlidingTabStrip);
171172

172173
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);
174175
underlineColor = a.getColor(R.styleable.PagerSlidingTabStrip_underlineColor, underlineColor);
175176
dividerColor = a.getColor(R.styleable.PagerSlidingTabStrip_dividerColor, dividerColor);
176177
indicatorHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_indicatorHeight, indicatorHeight);
178+
indicatorWidth = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_indicatorWidth, indicatorWidth);
177179
underlineHeight = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_underlineHeight, underlineHeight);
178180
dividerPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_dividerPadding, dividerPadding);
179181
tabPadding = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_tabPaddingLeftRight, tabPadding);
180182
tabBackgroundResId = a.getResourceId(R.styleable.PagerSlidingTabStrip_tabBackground, tabBackgroundResId);
181183
shouldExpand = a.getBoolean(R.styleable.PagerSlidingTabStrip_shouldExpand, shouldExpand);
184+
isFixedIndicatorWidth = a.getBoolean(R.styleable.PagerSlidingTabStrip_isFixedIndicatorWidth, isFixedIndicatorWidth);
182185
scrollOffset = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_scrollOffset, scrollOffset);
183186
// textAllCaps = a.getBoolean(R.styleable.PagerSlidingTabStrip_textAllCaps, textAllCaps);
184187
tabTextSize = a.getDimensionPixelSize(R.styleable.PagerSlidingTabStrip_tabDefTextSize, tabTextSize);
@@ -316,30 +319,18 @@ private void updateTabStyles() {
316319

317320
View v = tabsContainer.getChildAt(i);
318321

319-
v.setLayoutParams(defaultTabLayoutParams);
320322
v.setBackgroundResource(tabBackgroundResId);
321323

322324
if (v instanceof TextView) {
323325

324326
TextView tab = (TextView) v;
325327
tab.setTypeface(tabTypeface, tabTypefaceStyle);
326-
v.setPadding(tabPadding, 0, tabPadding, 0);
327328
if (currentIndex == i) {
328329
tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSizer);
329330
tab.setTextColor(tabTextColorr);
330-
// if (shouldExpand) {
331-
// v.setPadding(tabPadding, 0, tabPadding, 0);
332-
// } else {
333-
// v.setPadding(tabPadding, 0, tabPadding, 0);
334-
// }
335331
} else {
336332
tab.setTextSize(TypedValue.COMPLEX_UNIT_PX, tabTextSize);
337333
tab.setTextColor(tabTextColor);
338-
// if (shouldExpand) {
339-
// v.setPadding(0, 0, 0, 0);
340-
// } else {
341-
// v.setPadding(0, 0, 0, 0);
342-
// }
343334
}
344335
// setAllCaps() is only available from API 14, so the upper case
345336
// is made manually if we are on a
@@ -361,24 +352,27 @@ private void updateTabStyles() {
361352
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
362353
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
363354

364-
if (!shouldExpand
365-
|| MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED) {
366-
return;
367-
}
355+
// if (!shouldExpand
356+
// || MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.UNSPECIFIED) {
357+
// return;
358+
// }
368359

369360
int myWidth = getMeasuredWidth();
370361
int childWidth = 0;
371362
for (int i = 0; i < pageCount; i++) {
372-
childWidth += tabsContainer.getChildAt(i).getMeasuredWidth();
363+
childWidth += tabsContainer.getChildAt(i).getMeasuredWidth() + (tabPadding * 2);
373364
}
374365

375366
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);
382376
}
383377

384378
checkedTabWidths = true;
@@ -436,7 +430,9 @@ protected void onDraw(Canvas canvas) {
436430
lineRight = (currentPositionOffset * nextTabRight + (1f - currentPositionOffset)
437431
* lineRight);
438432
}
439-
433+
if (isFixedIndicatorWidth) {
434+
indicatorPadding = (lineRight - lineLeft - indicatorWidth) / 2;
435+
}
440436
canvas.drawRect(lineLeft + indicatorPadding, height - indicatorHeight, lineRight - indicatorPadding, height,
441437
rectPaint);
442438

0 commit comments

Comments
 (0)