Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use android studio's drop-down menu Spinner

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use android studio drop-down menu Spinner", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's take you to learn "how to use android studio drop-down menu Spinner"!

I know Spinner.

Spinner is actually a list selection box. However, Android's list selection box does not need to display a drop-down list, but rather a pop-up menu for users to choose from.

Spinner and Gallery both inherit AbsSpinner, and AbsSpinner inherits AdapterView, so it also exhibits AdapterView characteristics: just provide an Adapter for AdapterView.

1. related attributes

android:dropDownHorizontalOffset: android:dropDownSelector: background of the listbox when selected android:dropDownWidth: set width of the drop-down listbox android:gravity: set the way the components inside it android:popupBackground: set background of the listbox android:prompt: set prompt information (title) of the listbox in dialog mode, only resource id in string.xml can be referenced, and the string android:spinnerMode cannot be written directly: Mode of the listbox, with two selectable values: dialog: dialog-style window dropdown: drop-down menu style window (default) Optional properties: android:entries: use array resource settings drop-down listbox list items

If the developer can already determine the list items in the list selection box when using Spinner, there is no need to write code at all, just specify the android:entries attribute for Spinner to work properly; if the program needs to dynamically determine Spinner's list items at runtime, or if the program needs to customize Spinner's list items, you can use Adapter to provide list items for Spinner.

II. Spinner example

Let's learn how to use Spinner through a simple example program.

Continue to use the listviewsample module of WidgetSample project to create a spinner_layout.xml file in app/main/res/layout/directory and fill it with the following code snippet:

Create arrays.xml file under res/values/directory, define professionals array resources, as follows:

Next, provide an Adapter for Spinner. Create a new SpinnerActivity.java file and load the new layout file above. The specific code is as follows:

The above procedure is relatively simple, mainly for Spinner set selected settings listener.

Modify the Activity started by the program, run the program, and you can see the interface effect shown in the figure below.

Click on the first Spinner to pop up the selection dialog box,

This is shown below. Select one of them to return to the main screen and find that the value of Spinner will change to the selected content.

Similarly, click the second Spinner to open the drop-down list option box, as shown in the following figure.

Gallery and Spinner components have a common parent class: AbsSpinner, indicating that Gallery and Spinner are both a list selection box. The difference between them is that Spinner displays a vertical list selection box, while Gallery displays a horizontal list selection box. Gallery differs from Spinner in one other way: Spinner allows users to select items, while Gallery allows users to drag to see the previous and next list items.

The usage of Gallery itself is very simple-basically similar to the usage of Spinner, as long as you provide it with a content Adapter, the View returned by the getView() method of the Adapter will be the list item of Gallery list. If your program needs to monitor for changes to Gallery selections, you can do so by adding an OnltemSelectedListener listener to Gallery.

Android no longer recommends using Gallery components, but rather recommends using other horizontal scrolling components, such as HorizontalScrollView and ViewPager, instead of Gallery components, so I won't go into too much detail here

At this point, I believe everyone has a deeper understanding of "how to use android studio drop-down menu Spinner," may wish to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report