|
26 | 26 | import android.graphics.Rect; |
27 | 27 | import android.graphics.drawable.Drawable; |
28 | 28 | import android.util.AttributeSet; |
| 29 | +import android.util.DisplayMetrics; |
29 | 30 | import android.view.Gravity; |
30 | 31 | import android.view.View; |
31 | 32 | import android.view.ViewGroup; |
|
38 | 39 | * |
39 | 40 | * <p>See the <a href="{@docRoot}resources/tutorials/views/hello-spinner.html">Spinner |
40 | 41 | * tutorial</a>.</p> |
41 | | - * |
| 42 | + * |
42 | 43 | * @attr ref android.R.styleable#Spinner_prompt |
43 | 44 | */ |
44 | 45 | @Widget |
45 | 46 | public class Spinner extends AbsSpinner implements OnClickListener { |
46 | 47 | private static final String TAG = "Spinner"; |
47 | | - |
| 48 | + |
48 | 49 | // Only measure this many items to get a decent max width. |
49 | 50 | private static final int MAX_ITEMS_MEASURED = 15; |
50 | 51 |
|
51 | 52 | /** |
52 | 53 | * Use a dialog window for selecting spinner options. |
53 | 54 | */ |
54 | 55 | public static final int MODE_DIALOG = 0; |
55 | | - |
| 56 | + |
56 | 57 | /** |
57 | 58 | * Use a dropdown anchored to the Spinner for selecting spinner options. |
58 | 59 | */ |
@@ -732,26 +733,37 @@ public void setPromptText(CharSequence hintText) { |
732 | 733 |
|
733 | 734 | @Override |
734 | 735 | public void show() { |
| 736 | + final Drawable background = getBackground(); |
| 737 | + int bgOffset = 0; |
| 738 | + if (background != null) { |
| 739 | + background.getPadding(mTempRect); |
| 740 | + bgOffset = -mTempRect.left; |
| 741 | + } else { |
| 742 | + mTempRect.left = mTempRect.right = 0; |
| 743 | + } |
| 744 | + |
735 | 745 | final int spinnerPaddingLeft = Spinner.this.getPaddingLeft(); |
736 | 746 | if (mDropDownWidth == WRAP_CONTENT) { |
737 | 747 | final int spinnerWidth = Spinner.this.getWidth(); |
738 | 748 | final int spinnerPaddingRight = Spinner.this.getPaddingRight(); |
| 749 | + |
| 750 | + int contentWidth = measureContentWidth( |
| 751 | + (SpinnerAdapter) mAdapter, getBackground()); |
| 752 | + final int contentWidthLimit = mContext.getResources() |
| 753 | + .getDisplayMetrics().widthPixels - mTempRect.left - mTempRect.right; |
| 754 | + if (contentWidth > contentWidthLimit) { |
| 755 | + contentWidth = contentWidthLimit; |
| 756 | + } |
| 757 | + |
739 | 758 | setContentWidth(Math.max( |
740 | | - measureContentWidth((SpinnerAdapter) mAdapter, getBackground()), |
741 | | - spinnerWidth - spinnerPaddingLeft - spinnerPaddingRight)); |
| 759 | + contentWidth, spinnerWidth - spinnerPaddingLeft - spinnerPaddingRight)); |
742 | 760 | } else if (mDropDownWidth == MATCH_PARENT) { |
743 | 761 | final int spinnerWidth = Spinner.this.getWidth(); |
744 | 762 | final int spinnerPaddingRight = Spinner.this.getPaddingRight(); |
745 | 763 | setContentWidth(spinnerWidth - spinnerPaddingLeft - spinnerPaddingRight); |
746 | 764 | } else { |
747 | 765 | setContentWidth(mDropDownWidth); |
748 | 766 | } |
749 | | - final Drawable background = getBackground(); |
750 | | - int bgOffset = 0; |
751 | | - if (background != null) { |
752 | | - background.getPadding(mTempRect); |
753 | | - bgOffset = -mTempRect.left; |
754 | | - } |
755 | 767 | setHorizontalOffset(bgOffset + spinnerPaddingLeft); |
756 | 768 | setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED); |
757 | 769 | super.show(); |
|
0 commit comments