Skip to content

Commit 063c68f

Browse files
committed
Merge branch 'feat/align-and-padding'
2 parents e693cad + 24bda3d commit 063c68f

2 files changed

Lines changed: 32 additions & 1 deletion

File tree

library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,11 @@ public class NumberPicker extends LinearLayout {
156156
*/
157157
private static final int DEFAULT_TEXT_ALIGN = CENTER;
158158

159+
/**
160+
* The default content alignment.
161+
*/
162+
private static final int DEFAULT_ALIGN = CENTER;
163+
159164
/**
160165
* The default color of text.
161166
*/
@@ -630,6 +635,16 @@ private enum DividerType {
630635
*/
631636
private ViewConfiguration mViewConfiguration;
632637

638+
/**
639+
* The content alignment.
640+
*/
641+
private int mAlign = DEFAULT_ALIGN;
642+
643+
/**
644+
* The padding applied to the content when it's not centered (align is "left" or "right").
645+
*/
646+
private int mSidePadding = 0;
647+
633648
/**
634649
* Interface to listen for changes of the current value.
635650
*/
@@ -814,6 +829,9 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
814829
R.styleable.NumberPicker_np_hideWheelUntilFocused, false);
815830
mAccessibilityDescriptionEnabled = attributes.getBoolean(
816831
R.styleable.NumberPicker_np_accessibilityDescriptionEnabled, true);
832+
mAlign = attributes.getInt(R.styleable.NumberPicker_np_align, mAlign);
833+
mSidePadding = attributes.getDimensionPixelSize(
834+
R.styleable.NumberPicker_np_sidePadding, mSidePadding);
817835

818836
// By default LinearLayout that we extend is not drawn. This is
819837
// its draw() method is not called but dispatchDraw() is called
@@ -1767,7 +1785,14 @@ protected void onDraw(Canvas canvas) {
17671785
canvas.clipRect(mLeftDividerLeft, 0, mRightDividerRight, getBottom());
17681786
}
17691787
} else {
1770-
x = (getRight() - getLeft()) / 2f;
1788+
if (mAlign == LEFT) {
1789+
x = getLeft() + mSidePadding;
1790+
} else if (mAlign == RIGHT) {
1791+
x = getRight() - getMaxTextSize() - mSidePadding;
1792+
} else {
1793+
x = (getRight() - getLeft()) / 2f;
1794+
}
1795+
17711796
y = mCurrentScrollOffset;
17721797
if (mRealWheelItemCount < DEFAULT_WHEEL_ITEM_COUNT) {
17731798
canvas.clipRect(0, mTopDividerTop, getRight(), mBottomDividerBottom);

library/src/main/res/values/attrs.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,11 @@
5656
<attr name="np_value" format="integer" />
5757
<attr name="np_wheelItemCount" format="integer" />
5858
<attr name="np_wrapSelectorWheel" format="boolean" />
59+
<attr name="np_align" format="enum">
60+
<enum name="right" value="0" />
61+
<enum name="center" value="1" />
62+
<enum name="left" value="2" />
63+
</attr>
64+
<attr name="np_sidePadding" format="dimension" />
5965
</declare-styleable>
6066
</resources>

0 commit comments

Comments
 (0)