Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add parsing of independent padding attributes #69

Merged
merged 1 commit into from
Feb 21, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions library/src/com/astuetz/PagerSlidingTabStrip.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public interface OnTabReselectedListener {
android.R.attr.textColorPrimary,
android.R.attr.textSize,
android.R.attr.textColor,
android.R.attr.padding,
android.R.attr.paddingLeft,
android.R.attr.paddingRight,
};
Expand All @@ -76,8 +77,9 @@ public interface OnTabReselectedListener {
private static final int TEXT_COLOR_PRIMARY = 0;
private static final int TEXT_SIZE_INDEX = 1;
private static final int TEXT_COLOR_INDEX = 2;
private static final int PADDING_LEFT_INDEX = 3;
private static final int PADDING_RIGHT_INDEX = 4;
private static final int PADDING_INDEX = 3;
private static final int PADDING_LEFT_INDEX = 4;
private static final int PADDING_RIGHT_INDEX = 5;

private LinearLayout.LayoutParams defaultTabLayoutParams;
private LinearLayout.LayoutParams expandedTabLayoutParams;
Expand Down Expand Up @@ -113,7 +115,8 @@ public interface OnTabReselectedListener {
private float tabTextAlpha = HALF_TRANSP;
private float tabTextSelectedAlpha = OPAQUE;

private int padding = 0;
private int paddingLeft = 0;
private int paddingRight = 0;

private boolean shouldExpand = false;
private boolean textAllCaps = true;
Expand Down Expand Up @@ -170,13 +173,11 @@ public PagerSlidingTabStrip(Context context, AttributeSet attrs, int defStyle) {
underlineColor = textPrimaryColor;
dividerColor = textPrimaryColor;
indicatorColor = textPrimaryColor;
int paddingLeft = a.getDimensionPixelSize(PADDING_LEFT_INDEX, padding);
int paddingRight = a.getDimensionPixelSize(PADDING_RIGHT_INDEX, padding);
int padding = a.getDimensionPixelSize(PADDING_INDEX, 0);
paddingLeft = padding > 0 ? padding : a.getDimensionPixelSize(PADDING_LEFT_INDEX, 0);
paddingRight = padding > 0 ? padding : a.getDimensionPixelSize(PADDING_RIGHT_INDEX, 0);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as per typical Android views, if android:padding is given, it will override android:padding[Side]

a.recycle();

//In case we have the padding they must be equal so we take the biggest
padding = Math.max(paddingLeft, paddingRight);

// get custom attrs
a = context.obtainStyledAttributes(attrs, R.styleable.PagerSlidingTabStrip);
indicatorColor = a.getColor(R.styleable.PagerSlidingTabStrip_pstsIndicatorColor, indicatorColor);
Expand Down Expand Up @@ -370,7 +371,7 @@ private Pair<Float, Float> getIndicatorCoordinates() {

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (isPaddingMiddle || padding > 0) {
if (isPaddingMiddle || paddingLeft > 0 || paddingRight > 0) {
//Make sure tabContainer is bigger than the HorizontalScrollView to be able to scroll
tabsContainer.setMinimumWidth(getWidth());
//Clipping padding to false to see the tabs while we pass them swiping
Expand Down Expand Up @@ -400,10 +401,10 @@ public void onGlobalLayout() {

if (isPaddingMiddle) {
int mHalfWidthFirstTab = view.getWidth() / 2;
padding = getWidth() / 2 - mHalfWidthFirstTab;
paddingLeft = paddingRight = getWidth() / 2 - mHalfWidthFirstTab;
}
setPadding(padding, getPaddingTop(), padding, getPaddingBottom());
if (scrollOffset == 0) scrollOffset = getWidth() / 2 - padding;
setPadding(paddingLeft, getPaddingTop(), paddingRight, getPaddingBottom());
if (scrollOffset == 0) scrollOffset = getWidth() / 2 - paddingLeft;
}
};

Expand All @@ -418,10 +419,10 @@ protected void onDraw(Canvas canvas) {
// draw indicator line
rectPaint.setColor(indicatorColor);
Pair<Float, Float> lines = getIndicatorCoordinates();
canvas.drawRect(lines.first + padding, height - indicatorHeight, lines.second + padding, height, rectPaint);
canvas.drawRect(lines.first + paddingLeft, height - indicatorHeight, lines.second + paddingLeft, height, rectPaint);
// draw underline
rectPaint.setColor(underlineColor);
canvas.drawRect(padding, height - underlineHeight, tabsContainer.getWidth() + padding, height, rectPaint);
canvas.drawRect(paddingLeft, height - underlineHeight, tabsContainer.getWidth() + paddingRight, height, rectPaint);
// draw divider
if (dividerWidth != 0) {
dividerPaint.setStrokeWidth(dividerWidth);
Expand Down