|
26 | 26 | import android.graphics.Rect; |
27 | 27 | import android.graphics.drawable.Drawable; |
28 | 28 | import android.util.AttributeSet; |
| 29 | +import android.util.Log; |
29 | 30 | import android.view.Gravity; |
30 | 31 | import android.view.View; |
31 | 32 | import android.view.ViewGroup; |
@@ -202,6 +203,130 @@ public Spinner(Context context, AttributeSet attrs, int defStyle, int mode) { |
202 | 203 | } |
203 | 204 | } |
204 | 205 |
|
| 206 | + /** |
| 207 | + * Set the background drawable for the spinner's popup window of choices. |
| 208 | + * Only valid in {@link #MODE_DROPDOWN}; this method is a no-op in other modes. |
| 209 | + * |
| 210 | + * @param background Background drawable |
| 211 | + * |
| 212 | + * @attr ref android.R.styleable#Spinner_popupBackground |
| 213 | + */ |
| 214 | + public void setPopupBackgroundDrawable(Drawable background) { |
| 215 | + if (!(mPopup instanceof DropdownPopup)) { |
| 216 | + Log.e(TAG, "setPopupBackgroundDrawable: incompatible spinner mode; ignoring..."); |
| 217 | + return; |
| 218 | + } |
| 219 | + ((DropdownPopup) mPopup).setBackgroundDrawable(background); |
| 220 | + } |
| 221 | + |
| 222 | + /** |
| 223 | + * Set the background drawable for the spinner's popup window of choices. |
| 224 | + * Only valid in {@link #MODE_DROPDOWN}; this method is a no-op in other modes. |
| 225 | + * |
| 226 | + * @param background Resource ID of a background drawable |
| 227 | + * |
| 228 | + * @attr ref android.R.styleable#Spinner_popupBackground |
| 229 | + */ |
| 230 | + public void setPopupBackgroundResource(int resId) { |
| 231 | + setPopupBackgroundDrawable(getContext().getResources().getDrawable(resId)); |
| 232 | + } |
| 233 | + |
| 234 | + /** |
| 235 | + * Get the background drawable for the spinner's popup window of choices. |
| 236 | + * Only valid in {@link #MODE_DROPDOWN}; other modes will return null. |
| 237 | + * |
| 238 | + * @return background Background drawable |
| 239 | + * |
| 240 | + * @attr ref android.R.styleable#Spinner_popupBackground |
| 241 | + */ |
| 242 | + public Drawable getPopupBackground() { |
| 243 | + return mPopup.getBackground(); |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Set a vertical offset in pixels for the spinner's popup window of choices. |
| 248 | + * Only valid in {@link #MODE_DROPDOWN}; this method is a no-op in other modes. |
| 249 | + * |
| 250 | + * @param pixels Vertical offset in pixels |
| 251 | + * |
| 252 | + * @attr ref android.R.styleable#Spinner_dropDownVerticalOffset |
| 253 | + */ |
| 254 | + public void setDropDownVerticalOffset(int pixels) { |
| 255 | + mPopup.setVerticalOffset(pixels); |
| 256 | + } |
| 257 | + |
| 258 | + /** |
| 259 | + * Get the configured vertical offset in pixels for the spinner's popup window of choices. |
| 260 | + * Only valid in {@link #MODE_DROPDOWN}; other modes will return 0. |
| 261 | + * |
| 262 | + * @return Vertical offset in pixels |
| 263 | + * |
| 264 | + * @attr ref android.R.styleable#Spinner_dropDownVerticalOffset |
| 265 | + */ |
| 266 | + public int getDropDownVerticalOffset() { |
| 267 | + return mPopup.getVerticalOffset(); |
| 268 | + } |
| 269 | + |
| 270 | + /** |
| 271 | + * Set a horizontal offset in pixels for the spinner's popup window of choices. |
| 272 | + * Only valid in {@link #MODE_DROPDOWN}; this method is a no-op in other modes. |
| 273 | + * |
| 274 | + * @param pixels Horizontal offset in pixels |
| 275 | + * |
| 276 | + * @attr ref android.R.styleable#Spinner_dropDownHorizontalOffset |
| 277 | + */ |
| 278 | + public void setDropDownHorizontalOffset(int pixels) { |
| 279 | + mPopup.setHorizontalOffset(pixels); |
| 280 | + } |
| 281 | + |
| 282 | + /** |
| 283 | + * Get the configured horizontal offset in pixels for the spinner's popup window of choices. |
| 284 | + * Only valid in {@link #MODE_DROPDOWN}; other modes will return 0. |
| 285 | + * |
| 286 | + * @return Horizontal offset in pixels |
| 287 | + * |
| 288 | + * @attr ref android.R.styleable#Spinner_dropDownHorizontalOffset |
| 289 | + */ |
| 290 | + public int getDropDownHorizontalOffset() { |
| 291 | + return mPopup.getHorizontalOffset(); |
| 292 | + } |
| 293 | + |
| 294 | + /** |
| 295 | + * Set the width of the spinner's popup window of choices in pixels. This value |
| 296 | + * may also be set to {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT} |
| 297 | + * to match the width of the Spinner itself, or |
| 298 | + * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} to wrap to the measured size |
| 299 | + * of contained dropdown list items. |
| 300 | + * |
| 301 | + * <p>Only valid in {@link #MODE_DROPDOWN}; this method is a no-op in other modes.</p> |
| 302 | + * |
| 303 | + * @param pixels Width in pixels, WRAP_CONTENT, or MATCH_PARENT |
| 304 | + * |
| 305 | + * @attr ref android.R.styleable#Spinner_dropDownWidth |
| 306 | + */ |
| 307 | + public void setDropDownWidth(int pixels) { |
| 308 | + if (!(mPopup instanceof DropdownPopup)) { |
| 309 | + Log.e(TAG, "Cannot set dropdown width for MODE_DIALOG, ignoring"); |
| 310 | + return; |
| 311 | + } |
| 312 | + mDropDownWidth = pixels; |
| 313 | + } |
| 314 | + |
| 315 | + /** |
| 316 | + * Get the configured width of the spinner's popup window of choices in pixels. |
| 317 | + * The returned value may also be {@link android.view.ViewGroup.LayoutParams#MATCH_PARENT} |
| 318 | + * meaning the popup window will match the width of the Spinner itself, or |
| 319 | + * {@link android.view.ViewGroup.LayoutParams#WRAP_CONTENT} to wrap to the measured size |
| 320 | + * of contained dropdown list items. |
| 321 | + * |
| 322 | + * @return Width in pixels, WRAP_CONTENT, or MATCH_PARENT |
| 323 | + * |
| 324 | + * @attr ref android.R.styleable#Spinner_dropDownWidth |
| 325 | + */ |
| 326 | + public int getDropDownWidth() { |
| 327 | + return mDropDownWidth; |
| 328 | + } |
| 329 | + |
205 | 330 | @Override |
206 | 331 | public void setEnabled(boolean enabled) { |
207 | 332 | super.setEnabled(enabled); |
@@ -231,6 +356,16 @@ public void setGravity(int gravity) { |
231 | 356 | } |
232 | 357 | } |
233 | 358 |
|
| 359 | + /** |
| 360 | + * Describes how the selected item view is positioned. The default is determined by the |
| 361 | + * current theme. |
| 362 | + * |
| 363 | + * @return A {@link android.view.Gravity Gravity} value |
| 364 | + */ |
| 365 | + public int getGravity() { |
| 366 | + return mGravity; |
| 367 | + } |
| 368 | + |
234 | 369 | @Override |
235 | 370 | public void setAdapter(SpinnerAdapter adapter) { |
236 | 371 | super.setAdapter(adapter); |
@@ -675,6 +810,13 @@ private interface SpinnerPopup { |
675 | 810 | */ |
676 | 811 | public void setPromptText(CharSequence hintText); |
677 | 812 | public CharSequence getHintText(); |
| 813 | + |
| 814 | + public void setBackgroundDrawable(Drawable bg); |
| 815 | + public void setVerticalOffset(int px); |
| 816 | + public void setHorizontalOffset(int px); |
| 817 | + public Drawable getBackground(); |
| 818 | + public int getVerticalOffset(); |
| 819 | + public int getHorizontalOffset(); |
678 | 820 | } |
679 | 821 |
|
680 | 822 | private class DialogPopup implements SpinnerPopup, DialogInterface.OnClickListener { |
@@ -719,6 +861,36 @@ public void onClick(DialogInterface dialog, int which) { |
719 | 861 | } |
720 | 862 | dismiss(); |
721 | 863 | } |
| 864 | + |
| 865 | + @Override |
| 866 | + public void setBackgroundDrawable(Drawable bg) { |
| 867 | + Log.e(TAG, "Cannot set popup background for MODE_DIALOG, ignoring"); |
| 868 | + } |
| 869 | + |
| 870 | + @Override |
| 871 | + public void setVerticalOffset(int px) { |
| 872 | + Log.e(TAG, "Cannot set vertical offset for MODE_DIALOG, ignoring"); |
| 873 | + } |
| 874 | + |
| 875 | + @Override |
| 876 | + public void setHorizontalOffset(int px) { |
| 877 | + Log.e(TAG, "Cannot set horizontal offset for MODE_DIALOG, ignoring"); |
| 878 | + } |
| 879 | + |
| 880 | + @Override |
| 881 | + public Drawable getBackground() { |
| 882 | + return null; |
| 883 | + } |
| 884 | + |
| 885 | + @Override |
| 886 | + public int getVerticalOffset() { |
| 887 | + return 0; |
| 888 | + } |
| 889 | + |
| 890 | + @Override |
| 891 | + public int getHorizontalOffset() { |
| 892 | + return 0; |
| 893 | + } |
722 | 894 | } |
723 | 895 |
|
724 | 896 | private class DropdownPopup extends ListPopupWindow implements SpinnerPopup { |
|
0 commit comments