The article directories

  • First to use
  • Methods and Properties
  • style
    • Add the dividing line
    • Select and drop down styles
    • Popup style
  • Use a custom BaseAdapter

First to use

rendering

Layout the page activity_test

<? xml version="1.0" encoding="utf-8"? > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tv_tip"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="Mode of distribution" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/tv_tip"
            android:background="#E5E5E5"
            android:dropDownVerticalOffset="45dp"
            android:spinnerMode="dropdown" />

        <ImageView
            android:layout_width="10dp"
            android:layout_height="10dp"
            android:src="@mipmap/ic_triangle_down"
            android:layout_alignRight="@+id/spinner"
            android:layout_marginRight="10dp"
            android:layout_centerVertical="true"/>
    </RelativeLayout>
</LinearLayout>
Copy the code

code

public class TestActivity extends AppCompatActivity {
    private String[] starArray = {"Yuantong"."Shentong"."Postal"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        initSpinner();
    }

    private void initSpinner(a) {
        // Declare an array adapter for drop-down lists
        ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this, R.layout.item_select, starArray);
        // Sets the layout style of the array adapter
        starAdapter.setDropDownViewResource(R.layout.item_dropdown);
        // Get a drop - down box named sp_dialog from the layout file
        Spinner sp = findViewById(R.id.spinner);
        // Set the title of the drop-down box. If you do not set it, there is no ugly title
        sp.setPrompt("Please select delivery mode");
        // Set the array adapter for the drop-down box
        sp.setAdapter(starAdapter);
        // Set the drop-down box to display the first item by default
        sp.setSelection(0);
        // Set a selection listener for the dropdown box. Once the user selects an item, the listener's onItemSelected method is triggered
        sp.setOnItemSelectedListener(new MySelectedListener());
    }

    class MySelectedListener implements AdapterView.OnItemSelectedListener {

        @Override
        public void onItemSelected(AdapterView<? > adapterView, View view,int i, long l) {
            Toast.makeText(TestActivity.this."What did you choose?" + starArray[i], Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView
        adapterView) {}}}Copy the code

The layout Item_drapDown is not selected

<? xml version="1.0" encoding="utf-8"? > <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:textColor="@android:color/black"
    android:textSize="14sp"
    android:gravity="center"/>
Copy the code

Select the layout item_SELECT

<? xml version="1.0" encoding="utf-8"? > <TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#E5E5E5"
    android:textSize="14sp"
    android:gravity="center"/>
Copy the code

Methods and Properties

SetPrompt: Sets the title text. There are two ways to display the drop-down list. One is to display the list directly below the current drop-down box and set the spinnerMode property to Dropdown. The other option is to display the list as a dialog box in the middle of the page, in which case the SpinnerMode property is set to dialog. This setPrompt method sets the title setAdapter at dialog time: the adapter that sets the drop-down list. SetSelection: Sets the current selection. Note that this method is called after the setAdapter method. SetOnItemSelectedListener: set drop-down list a selection listener, the listener OnItemSelectedListener to implement interface. Android: dropDownVerticalOffset: spinnerMode = "dropdown can", the drop-down selection window of projects in the vertical direction relative to the Spinner window offset android: dropDownHorizontalOffset: SpinnerMode = "dropdown", the horizontal offset of the dropdown item selection window relative to the Spinner window Android :gravity :dropDownWidth android:gravity :dropDownWidth android:gravity :dropDownWidth This property is used to set the alignment of the currently selected item android:popupBackground: When spinner= "dropdown", this property is used to set the background of the drop-down listCopy the code

If the contents of a spinner are fixed, then android:entries=”@array/shipping_type” indicates that the data set of a spinner is retrieved from the resource array shipping_type. The Shipping_type array resource is defined in values/arrays.xml:

<Spinner
            android:id="@+id/spinner"
            android:layout_width="200dp"
            android:layout_height="40dp"
            android:layout_marginLeft="10dp"
            android:layout_toRightOf="@+id/tv_tip"
            android:background="#E5E5E5"
            android:dropDownVerticalOffset="0dp"
            android:spinnerMode="dropdown"
            android:entries="@arrays/shipping_type"
            />
Copy the code

The shipping_type

<? xml version="1.0" encoding="utf-8"? > <resources> <string-array name="shipping_type"> <item> Yto </item> <item> Shto </item> <item> Post </item> </string-array> </resources>Copy the code

style

Add the dividing line

This line is only displayed if it is dropdown style



Add a separator line style to the style

<style name="XSpinnerStyle" parent="android:Widget.ListView.DropDown">
        <! -- Separator line color -->
        <item name="android:divider">#E5E5E5</item>
        <item name="android:dividerHeight">1dp</item>
    </style>
Copy the code

Then use it in the style that the Activity is currently using

<item name="android:dropDownListViewStyle">@style/XSpinnerStyle</item>
Copy the code

Select and drop down styles

ArrayAdapter<String> starAdapter = new ArrayAdapter<String>(this, R.layout.item_select, starArray);
starAdapter.setDropDownViewResource(R.layout.item_dropdown);
Copy the code

Item_select and r.layout. item_dropdown are selected and dropdown styles. This is the style we wrote ourselves. You can also use the default style. Simple_spinner_item: Spinner’s default style when the menu is not expanded Android.r.layout. simple_spinner_dropdown_item: Dropdown style when the menu is expanded

If not setstarAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)The result is the same drop-down style as when it was expanded

Popup style

In the first chestnut, set the SpinnerMode property to dialog

Use a custom BaseAdapter

Effect:

The layout is the same activity_test as before

TestActivity

public class TestActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        // Initialize the controller
        Spinner spinner  = findViewById(R.id.spinner);
        // Create the data source
        List<ShippingType> shippingTypes =new ArrayList<ShippingType>();
        shippingTypes.add(new ShippingType("Yuantong", R.mipmap.yt));
        shippingTypes.add(new ShippingType("Shentong", R.mipmap.st));
        shippingTypes.add(new ShippingType("Postal", R.mipmap.yz));
        // Create Adapter binding data source
        MyAdapter _MyAdapter=new MyAdapter(this, shippingTypes);
        / / bind Adapterspinner.setAdapter(_MyAdapter); }}Copy the code

ShippingType

public class ShippingType {
    private String shippingName;
    private int shippingIcon;

    public ShippingType(String shippingName, int shippingIcon) {
        this.shippingName = shippingName;
        this.shippingIcon = shippingIcon;
    }

    public String getShippingName(a) {
        return shippingName;
    }

    public void setShippingName(String shippingName) {
        this.shippingName = shippingName;
    }

    public int getShippingIcon(a) {
        return shippingIcon;
    }

    public void setShippingIcon(int shippingIcon) {
        this.shippingIcon = shippingIcon; }}Copy the code

item_custom

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:gravity="center">
    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:src="@mipmap/yt"
        android:layout_marginRight="10dp"/>
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>
Copy the code

The android Spinner controls are used in detail