我不确定这个答案,您可以接受或忽略它。
我认为通过主题更改无法实现此要求。因为 Spinner 构造函数仅在您在布局 xml 中编写时才在 popupBackground attr 上分配值,否则它将使用默认主题值。如下所示:
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:popupBackground="@android:color/holo_green_dark" />
我附上了微调器构造函数的源代码,所以你会得到更好的知识。
/**
* Construct a new spinner with the given context's theme, the supplied attribute set,
* and default style. <code>mode</code> may be one of {@link #MODE_DIALOG} or
* {@link #MODE_DROPDOWN} and determines how the user will select choices from the spinner.
*
* @param context The Context the view is running in, through which it can
* access the current theme, resources, etc.
* @param attrs The attributes of the XML tag that is inflating the view.
* @param defStyle The default style to apply to this view. If 0, no style
* will be applied (beyond what is included in the theme). This may
* either be an attribute resource, whose value will be retrieved
* from the current theme, or an explicit style resource.
* @param mode Constant describing how the user will select choices from the spinner.
*
* @see #MODE_DIALOG
* @see #MODE_DROPDOWN
*/
public Spinner(Context context, AttributeSet attrs, int defStyle, int mode) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
com.android.internal.R.styleable.Spinner, defStyle, 0);
if (mode == MODE_THEME) {
mode = a.getInt(com.android.internal.R.styleable.Spinner_spinnerMode, MODE_DIALOG);
}
switch (mode) {
case MODE_DIALOG: {
mPopup = new DialogPopup();
break;
}
case MODE_DROPDOWN: {
DropdownPopup popup = new DropdownPopup(context, attrs, defStyle);
mDropDownWidth = a.getLayoutDimension(
com.android.internal.R.styleable.Spinner_dropDownWidth,
ViewGroup.LayoutParams.WRAP_CONTENT);
popup.setBackgroundDrawable(a.getDrawable(
com.android.internal.R.styleable.Spinner_popupBackground));
final int verticalOffset = a.getDimensionPixelOffset(
com.android.internal.R.styleable.Spinner_dropDownVerticalOffset, 0);
if (verticalOffset != 0) {
popup.setVerticalOffset(verticalOffset);
}
final int horizontalOffset = a.getDimensionPixelOffset(
com.android.internal.R.styleable.Spinner_dropDownHorizontalOffset, 0);
if (horizontalOffset != 0) {
popup.setHorizontalOffset(horizontalOffset);
}
mPopup = popup;
break;
}
}
mGravity = a.getInt(com.android.internal.R.styleable.Spinner_gravity, Gravity.CENTER);
mPopup.setPromptText(a.getString(com.android.internal.R.styleable.Spinner_prompt));
mDisableChildrenWhenDisabled = a.getBoolean(
com.android.internal.R.styleable.Spinner_disableChildrenWhenDisabled, false);
a.recycle();
// Base constructor can call setAdapter before we initialize mPopup.
// Finish setting things up if this happened.
if (mTempAdapter != null) {
mPopup.setAdapter(mTempAdapter);
mTempAdapter = null;
}
}