Merge "Set width of spinner's dropdown to fit on device"
This commit is contained in:
@@ -26,6 +26,7 @@ import android.database.DataSetObserver;
|
|||||||
import android.graphics.Rect;
|
import android.graphics.Rect;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
import android.util.DisplayMetrics;
|
||||||
import android.view.Gravity;
|
import android.view.Gravity;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
@@ -38,13 +39,13 @@ import android.view.ViewGroup;
|
|||||||
*
|
*
|
||||||
* <p>See the <a href="{@docRoot}resources/tutorials/views/hello-spinner.html">Spinner
|
* <p>See the <a href="{@docRoot}resources/tutorials/views/hello-spinner.html">Spinner
|
||||||
* tutorial</a>.</p>
|
* tutorial</a>.</p>
|
||||||
*
|
*
|
||||||
* @attr ref android.R.styleable#Spinner_prompt
|
* @attr ref android.R.styleable#Spinner_prompt
|
||||||
*/
|
*/
|
||||||
@Widget
|
@Widget
|
||||||
public class Spinner extends AbsSpinner implements OnClickListener {
|
public class Spinner extends AbsSpinner implements OnClickListener {
|
||||||
private static final String TAG = "Spinner";
|
private static final String TAG = "Spinner";
|
||||||
|
|
||||||
// Only measure this many items to get a decent max width.
|
// Only measure this many items to get a decent max width.
|
||||||
private static final int MAX_ITEMS_MEASURED = 15;
|
private static final int MAX_ITEMS_MEASURED = 15;
|
||||||
|
|
||||||
@@ -52,7 +53,7 @@ public class Spinner extends AbsSpinner implements OnClickListener {
|
|||||||
* Use a dialog window for selecting spinner options.
|
* Use a dialog window for selecting spinner options.
|
||||||
*/
|
*/
|
||||||
public static final int MODE_DIALOG = 0;
|
public static final int MODE_DIALOG = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use a dropdown anchored to the Spinner for selecting spinner options.
|
* Use a dropdown anchored to the Spinner for selecting spinner options.
|
||||||
*/
|
*/
|
||||||
@@ -732,13 +733,30 @@ public class Spinner extends AbsSpinner implements OnClickListener {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void show() {
|
public void show() {
|
||||||
|
final Drawable background = getBackground();
|
||||||
|
int bgOffset = 0;
|
||||||
|
if (background != null) {
|
||||||
|
background.getPadding(mTempRect);
|
||||||
|
bgOffset = -mTempRect.left;
|
||||||
|
} else {
|
||||||
|
mTempRect.left = mTempRect.right = 0;
|
||||||
|
}
|
||||||
|
|
||||||
final int spinnerPaddingLeft = Spinner.this.getPaddingLeft();
|
final int spinnerPaddingLeft = Spinner.this.getPaddingLeft();
|
||||||
if (mDropDownWidth == WRAP_CONTENT) {
|
if (mDropDownWidth == WRAP_CONTENT) {
|
||||||
final int spinnerWidth = Spinner.this.getWidth();
|
final int spinnerWidth = Spinner.this.getWidth();
|
||||||
final int spinnerPaddingRight = Spinner.this.getPaddingRight();
|
final int spinnerPaddingRight = Spinner.this.getPaddingRight();
|
||||||
|
|
||||||
|
int contentWidth = measureContentWidth(
|
||||||
|
(SpinnerAdapter) mAdapter, getBackground());
|
||||||
|
final int contentWidthLimit = mContext.getResources()
|
||||||
|
.getDisplayMetrics().widthPixels - mTempRect.left - mTempRect.right;
|
||||||
|
if (contentWidth > contentWidthLimit) {
|
||||||
|
contentWidth = contentWidthLimit;
|
||||||
|
}
|
||||||
|
|
||||||
setContentWidth(Math.max(
|
setContentWidth(Math.max(
|
||||||
measureContentWidth((SpinnerAdapter) mAdapter, getBackground()),
|
contentWidth, spinnerWidth - spinnerPaddingLeft - spinnerPaddingRight));
|
||||||
spinnerWidth - spinnerPaddingLeft - spinnerPaddingRight));
|
|
||||||
} else if (mDropDownWidth == MATCH_PARENT) {
|
} else if (mDropDownWidth == MATCH_PARENT) {
|
||||||
final int spinnerWidth = Spinner.this.getWidth();
|
final int spinnerWidth = Spinner.this.getWidth();
|
||||||
final int spinnerPaddingRight = Spinner.this.getPaddingRight();
|
final int spinnerPaddingRight = Spinner.this.getPaddingRight();
|
||||||
@@ -746,12 +764,6 @@ public class Spinner extends AbsSpinner implements OnClickListener {
|
|||||||
} else {
|
} else {
|
||||||
setContentWidth(mDropDownWidth);
|
setContentWidth(mDropDownWidth);
|
||||||
}
|
}
|
||||||
final Drawable background = getBackground();
|
|
||||||
int bgOffset = 0;
|
|
||||||
if (background != null) {
|
|
||||||
background.getPadding(mTempRect);
|
|
||||||
bgOffset = -mTempRect.left;
|
|
||||||
}
|
|
||||||
setHorizontalOffset(bgOffset + spinnerPaddingLeft);
|
setHorizontalOffset(bgOffset + spinnerPaddingLeft);
|
||||||
setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
|
setInputMethodMode(ListPopupWindow.INPUT_METHOD_NOT_NEEDED);
|
||||||
super.show();
|
super.show();
|
||||||
|
|||||||
Reference in New Issue
Block a user