From ab7c6bbf41f16c3dff09e6de5f5dff6d3a84f1c1 Mon Sep 17 00:00:00 2001
From: H07000223 <867318349@qq.com>
Date: Tue, 10 Nov 2015 17:48:44 +0800
Subject: [PATCH] new added attr tl_indicator_width_equal_title
---
.../com/flyco/tablayout/CommonTabLayout.java | 12 ++++++----
.../com/flyco/tablayout/SlidingTabLayout.java | 23 +++++++++++++++----
.../src/main/res/values/attrs.xml | 3 +++
.../main/res/layout/activity_sliding_tab.xml | 1 +
4 files changed, 30 insertions(+), 9 deletions(-)
diff --git a/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/CommonTabLayout.java b/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/CommonTabLayout.java
index ce5ba58e..6bea6c51 100644
--- a/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/CommonTabLayout.java
+++ b/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/CommonTabLayout.java
@@ -453,11 +453,15 @@ protected void onDraw(Canvas canvas) {
if (indicatorHeight > 0) {
indicatorDrawable.setColor(indicatorColor);
if (indicatorGravity == Gravity.BOTTOM) {
- indicatorDrawable.setBounds(paddingLeft + indicatorRect.left,
- (int) (height - indicatorHeight), paddingLeft + indicatorRect.right, height);
+ indicatorDrawable.setBounds(paddingLeft + (int) indicatorMarginLeft + indicatorRect.left,
+ height - (int) indicatorHeight - (int) indicatorMarginBottom,
+ paddingLeft + indicatorRect.right - (int) indicatorMarginRight,
+ height - (int) indicatorMarginBottom);
} else {
- indicatorDrawable.setBounds(paddingLeft + indicatorRect.left,
- 0, paddingLeft + indicatorRect.right, (int) indicatorHeight);
+ indicatorDrawable.setBounds(paddingLeft + (int) indicatorMarginLeft + indicatorRect.left,
+ (int) indicatorMarginTop,
+ paddingLeft + indicatorRect.right - (int) indicatorMarginRight,
+ (int) indicatorHeight + (int) indicatorMarginTop);
}
indicatorDrawable.setCornerRadius(indicatorCornerRadius);
indicatorDrawable.draw(canvas);
diff --git a/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java b/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java
index be1fd653..fbf0cdab 100644
--- a/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java
+++ b/FlycoTabLayout_Lib/src/main/java/com/flyco/tablayout/SlidingTabLayout.java
@@ -70,6 +70,7 @@ public class SlidingTabLayout extends HorizontalScrollView implements ViewPager.
private float indicatorMarginRight;
private float indicatorMarginBottom;
private float indicatorGravity;
+ private boolean indicatorWidthEqualTitle;
/** underline */
private int underlineColor;
@@ -140,6 +141,7 @@ private void obtainAttributes(Context context, AttributeSet attrs) {
indicatorMarginRight = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_margin_right, dp2px(0));
indicatorMarginBottom = ta.getDimension(R.styleable.SlidingTabLayout_tl_indicator_margin_bottom, dp2px(indicatorStyle == STYLE_BLOCK ? 7 : 0));
indicatorGravity = ta.getInt(R.styleable.SlidingTabLayout_tl_indicator_gravity, Gravity.BOTTOM);
+ indicatorWidthEqualTitle = ta.getBoolean(R.styleable.SlidingTabLayout_tl_indicator_width_equal_title, false);
underlineColor = ta.getColor(R.styleable.SlidingTabLayout_tl_underline_color, Color.parseColor("#ffffff"));
underlineHeight = ta.getDimension(R.styleable.SlidingTabLayout_tl_underline_height, dp2px(0));
@@ -377,7 +379,9 @@ private void calcIndicatorRect() {
tabRect.right = (int) right;
if (indicatorWidth < 0) { //indicatorWidth小于0时,原jpardogo's PagerSlidingTabStrip
-
+ if (indicatorStyle == STYLE_NORMAL && indicatorWidthEqualTitle) {//只有在STYLE_NORMAL并且indicatorWidth小于零有效
+ indicatorMarginLeft = indicatorMarginRight = tabPadding - 1;
+ }
} else {//indicatorWidth大于0时,圆角矩形以及三角形
float indicatorLeft = currentTabView.getLeft() + (currentTabView.getWidth() - indicatorWidth) / 2;
@@ -462,11 +466,15 @@ protected void onDraw(Canvas canvas) {
if (indicatorHeight > 0) {
indicatorDrawable.setColor(indicatorColor);
if (indicatorGravity == Gravity.BOTTOM) {
- indicatorDrawable.setBounds(paddingLeft + indicatorRect.left,
- (int) (height - indicatorHeight), paddingLeft + indicatorRect.right, height);
+ indicatorDrawable.setBounds(paddingLeft + (int) indicatorMarginLeft + indicatorRect.left,
+ height - (int) indicatorHeight - (int) indicatorMarginBottom,
+ paddingLeft + indicatorRect.right - (int) indicatorMarginRight,
+ height - (int) indicatorMarginBottom);
} else {
- indicatorDrawable.setBounds(paddingLeft + indicatorRect.left,
- 0, paddingLeft + indicatorRect.right, (int) indicatorHeight);
+ indicatorDrawable.setBounds(paddingLeft + (int) indicatorMarginLeft + indicatorRect.left,
+ (int) indicatorMarginTop,
+ paddingLeft + indicatorRect.right - (int) indicatorMarginRight,
+ (int) indicatorHeight + (int) indicatorMarginTop);
}
indicatorDrawable.setCornerRadius(indicatorCornerRadius);
indicatorDrawable.draw(canvas);
@@ -544,6 +552,11 @@ public void setIndicatorMargin(float indicatorMarginLeft, float indicatorMarginT
invalidate();
}
+ public void setIndicatorWidthEqualTitle(boolean indicatorWidthEqualTitle) {
+ this.indicatorWidthEqualTitle = indicatorWidthEqualTitle;
+ invalidate();
+ }
+
public void setUnderlineGravity(float underlineGravity) {
this.underlineGravity = underlineGravity;
invalidate();
diff --git a/FlycoTabLayout_Lib/src/main/res/values/attrs.xml b/FlycoTabLayout_Lib/src/main/res/values/attrs.xml
index 0fa5970e..7fd53cdb 100644
--- a/FlycoTabLayout_Lib/src/main/res/values/attrs.xml
+++ b/FlycoTabLayout_Lib/src/main/res/values/attrs.xml
@@ -25,6 +25,8 @@
+
+
@@ -77,6 +79,7 @@
+
diff --git a/app/src/main/res/layout/activity_sliding_tab.xml b/app/src/main/res/layout/activity_sliding_tab.xml
index 20956545..da2a9fdc 100644
--- a/app/src/main/res/layout/activity_sliding_tab.xml
+++ b/app/src/main/res/layout/activity_sliding_tab.xml
@@ -38,6 +38,7 @@
tl:tl_divider_width="1dp"
tl:tl_indicator_color="#000000"
tl:tl_indicator_height="1.5dp"
+ tl:tl_indicator_width_equal_title="true"
tl:tl_tab_padding="22dp"
tl:tl_tab_space_equal="true"
tl:tl_textSelectColor="#000000"