简体   繁体   English

如何设置 Android 微调器弹出窗口的样式?

[英]How to style Android spinner popup?

I have set我已经设定

<item name="android:spinnerMode">dialog</item>

and so when I tap the spinner, I get a popup.所以当我点击微调器时,我会看到一个弹出窗口。 But that popup is grey with white text and I can't seem to change any of the colors. How do I style this dialog?但是那个弹出窗口是灰色的,带有白色文本,我似乎无法更改 colors 中的任何一个。我该如何设置这个对话框的样式?

I tried the following with some crazy temporary colors to see what changes but nothing changes.我尝试了以下一些疯狂的临时 colors 来查看有什么变化但没有任何变化。

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:dialogTheme">@style/SpinnerDialog</item>
    <item name="android:alertDialogTheme">@style/SpinnerAlertDialog</item>
</style>

<style name="SpinnerDialog" parent="Theme.AppCompat.Light.Dialog">
    <item name="android:popupBackground">#ff00ff</item>
    <item name="colorPrimary">#ff00ff</item>
    <item name="colorPrimaryDark">#ffff00</item>
    <item name="colorAccent">#ff0000</item>
</style>

<style name="SpinnerAlertDialog" parent="Theme.AppCompat.Dialog">
    <item name="colorPrimary">#00ffff</item>
    <item name="colorPrimaryDark">#00ff00</item>
    <item name="colorAccent">#0000ff</item>
</style>

There are a bunch of similar questions, but they all either deal with dropdowns or ancient versions of Android or just don't work.有很多类似的问题,但它们要么处理下拉菜单,要么处理 Android 的旧版本,或者根本不起作用。

Instead of using theme or style.xml for changing the popup background color of dialog.而不是使用 theme 或 style.xml 来更改对话框的弹出背景颜色。

Why not try this??为什么不试试这个?? In your layout xml在您的布局 xml 中

 <Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:spinnerMode="dialog"
    android:popupBackground="#yourcolor"/>

Since you tried adding theme it changes nothing.This will be easy to achieve..isn't it??由于您尝试添加主题,因此它没有任何改变。这很容易实现..不是吗??

Hope this helps!!!希望这可以帮助!!!

You can use a custom layout to achieve this.您可以使用自定义布局来实现此目的。

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
        R.id.custom_spinner_item, yourItemList);
adapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
spinner.setAdapter(adapter);

You should have the custom layouts in place:您应该有自定义布局:

  • R.id.custom_spinner_item for the item in the spinner. R.id.custom_spinner_item用于微调器中的项目。
  • R.layout.custom_spinner_dropdown_item for the spinner drop-down item. R.layout.custom_spinner_dropdown_item用于微调下拉项。

I wanted to change the dialog background and add custom padding here's the style:我想更改对话框背景并在此处添加自定义填充样式:

 <style name="customSpinnerDialog" >

        <item name="android:background">@android:color/white</item>
        <item name="android:textColor">@color/color_accent</item>
        <item name="android:padding">0dp</item>

 </style>

to apply to you spinner in layout xml :在布局 xml 中应用于您的微调器:

<android.support.v7.widget.AppCompatSpinner
                android:id="@+id/reason_spinner"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:drawSelectorOnTop="true"
                android:theme="@style/customSpinnerDialog"
                android:spinnerMode="dialog"
                style="@style/SpinnerTheme"
                 />

hope this helps.希望这可以帮助。

Just create theme with "background" item which contains color that you want.只需使用包含您想要的颜色的“背景”项目创建主题。

<style name="Spinner.PopUpTheme">
    <item name="android:background">@color/black</item>
</style>

Then set that theme to "popupTheme" attribute for your spinner.然后将该主题设置为微调器的“popupTheme”属性。

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:spinnerMode="dialog"
    android:popupTheme="Spinner.PopUpTheme"/>

in your app theme or activity them :在您的应用主题或活动中:

  <item name="android:dropDownListViewStyle">@style/SpinnerStyle1</item>

declare SpinnerStyle1:声明 SpinnerStyle1:

<style name="SpinnerStyle1" parent="Widget.AppCompat.ListView.DropDown">
        <item name="android:divider">@color/blackText</item>
        <item name="android:dividerHeight">1px</item>
        <item name="android:scrollbars">none</item>
</style>

If it is BaseAdapter, you can use parent's rootView to set background.如果是BaseAdapter,可以使用parent的rootView设置背景。

@SuppressLint("ViewHolder")
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {

    val view: View = inflater.inflate(R.layout.unit_spinner_item, parent, false)
    parent?.rootView?.background = AppCompatResources.getDrawable(context,R.drawable.aa_shape )

    ...

    return view
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM