Android studio drop-down menu spinner usage details

1、 Meet spinner

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

Both spinner and gallery inherit absspinner, and absspinner inherits AdapterView, so it also shows the characteristics of AdapterView: as long as an adapter is provided for AdapterView.

1. Related attributes

If the developer can 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 make spinner work normally; If the program needs to dynamically determine the list items of Spinner at run time, or the program needs to customize the list items of spinner, you can use the adapter to provide list items for spinner.

2、 Spinner example

Next, learn how to use spinner through a simple example program.

Continue to use the listviewsample module of the widgetsample project and create a spinner in the app / main / RES / layout / directory_ Layout.xml file, filled with the following code fragments:

Create a new arrays.xml file in the RES / values / directory and define the professionals array resources, as follows:

Next, provide an adapter for spinner. Create a new spinneractivity.java file and load the layout file created above. The specific code is as follows:

The above program is relatively simple. It mainly sets the selected listener for spinner.

Modify the activity started by the program and run the program to see the interface effect shown in the figure below.

Click the first spinner to pop up the selection dialog box,

As shown in the figure below. Select one of them to return to the main interface, 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 both gallery and spinner are 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. There is another difference between gallery and spinner: spinner is used for users to select, while Gallery allows users to view the previous and next list items by dragging.

The usage of gallery itself is very simple - basically similar to that of spinner. Just provide it with a content adapter, and the view returned by the getview () method of the adapter will be used as the list item of the gallery list. If the program needs to monitor the change of gallery selection, it can be realized by adding onltemselectedlistener listener to gallery.

Android no longer recommends using the gallery component, but recommends using other horizontal Scrollview components, such as horizontalscrollview and viewpager, instead of the gallery component, so we won't explain it here

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>