set spinner mode in XML

2019-04-09 21:28发布

When defining a spinner in code, you can set the mode to 'dialog' or 'dropdown':

Spinner(Context context, int mode) Construct a new spinner with the given context's theme and the supplied mode of displaying choices.

But I can't find this option when defining my layout in XML. Did I just miss it, or is this not possible in XML?

4条回答
可以哭但决不认输i
2楼-- · 2019-04-09 21:34

To use SpinnerMode Xml attribute and work on API Level 11 or higher.

you need to create your own style for spinner.

1] put in themes.xml file in values Folder :

<style name="spinner_style" >
    <item name="spinnerMode">dialog</item>
</style>

2] put in themes.xml file in values-v11 Folder and values-v14 Folder :

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

3] then use your style in Spinner xml tag

<Spinner android:id="@+id/my_spinner"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        style="@style/spinner_style"/>
查看更多
等我变得足够好
3楼-- · 2019-04-09 21:41

No, according to the reference found here this is not possible. There is no corresponding XML attribute listed. Like other things as setting 24h mode for a timepicker, which is not possible in XML.

查看更多
可以哭但决不认输i
4楼-- · 2019-04-09 21:43

If you're using the API level 10 or lower just remove android:spinnerMode and style from your XML file.

查看更多
闹够了就滚
5楼-- · 2019-04-09 21:44

As of API level 11, you can use

<Spinner style="@android:style/Widget.Spinner.DropDown" ... />

or

<Spinner android:spinnerMode="dropdown" ... />
查看更多
登录 后发表回答