【问题标题】:Change background of spinner popup, Android 4.0更改微调器弹出窗口的背景,Android 4.0
【发布时间】:2013-03-26 11:18:09
【问题描述】:

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

【问题讨论】:

  • 使用自定义适配器我希望它会做
  • 感谢您的建议,但我想用一个主题来做,因为我想为我的应用程序中的每个微调器定义默认样式,就像我已经为很多事情所做的那样。
  • 弹出窗口不属于微调器,它是一个适配器,您将使用类似 android.layout.simplespinneritem1 .. 由 android 定义的资源膨胀并显示其背景跨度>

标签: android background spinner customization


【解决方案1】:

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

我认为这个要求不能通过主题更改来实现。因为 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;
    }
}

【讨论】:

  • 这行得通! :) 由于论坛上此类问题的问题(和答案),我被引导到主题使用,但在 xml 中添加 'android:popupBackground' 解决了它。非常感谢你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-26
相关资源
最近更新 更多