我想更改单击微调器时出现的弹出窗口的背景。我查看了论坛并查看了android源码,但没有任何对我有用,背景仍然是灰色的,因为它是默认情况下。到目前为止,我已经对弹出窗口进行了自定义,当弹出操作栏的溢出并且一切正常时都会出现,但是我在使用这个Spinner小部件时遇到了麻烦。我已经尝试过了:
<style name="AppTheme.Dark" parent="@android:style/Theme.Holo">
<item name="android:spinnerStyle">@style/SpinnerDropDownItem</item>
</style>
<style name="SpinnerDropDownItem" parent="@android:style/Widget.Spinner">
<item name="android:popupBackground">@drawable/menu_dropdown_panel_actinonbarstyle</item>
</style>
以及我在源代码中搜索过的其他组合。有人可以帮我,用代码示例,我怎么能这样做,因为我真的被卡住了。我正在开发Android 4.0及更高版本的应用程序。 THX
答案 0 :(得分:4)
我不确定这个答案,你可以接受或忽略它。
我认为通过主题更改无法实现此要求。因为只有在layout xml中写入时,Spinner构造函数才会在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;
}
}