0

我想更改单击微调器后出现的弹出窗口的背景。我查看了论坛并查看了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 及更高版本开发我的应用程序。谢谢

4

1 回答 1

4

在此处输入图像描述

我不确定这个答案,您可以接受或忽略它。

我认为通过主题更改无法实现此要求。因为 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;
    }
}
于 2013-03-26T13:07:39.923 回答