简单实用
Spinner是下拉弹出框,类似popWindow,但是Spinner有2种模式,dialog + dropdown。dropdown是在spinner的下方显示,dialog是作为一个dialog显示。
效果图
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.cqc.spinneradaptger.MainActivity">
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
android:text="Hello World!"/>
</RelativeLayout>
代码:
package com.cqc.spinneradaptger;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private String[] array = {"item1","item2","item3"};
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(new MyAdapter());//adapter继承BaseAdapter
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//获取选中的值
String str1 = (String) parent.getItemAtPosition(position);
String str2 = (String) spinner.getItemAtPosition(position);
Log.d("tag","str1="+str1+"--str2="+str2);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
parent.setVisibility(View.VISIBLE);//可以不写
}
});
}
public class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return array.length;
}
@Override
public Object getItem(int position) {
return array[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(MainActivity.this);
tv.setTextSize(20.0f);
tv.setText(array[position]);
return tv;
}
}
}
源码:
官方API使用指南
官方API:https://developer.android.google.cn/guide/topics/ui/controls/spinner.html
<!--如果我们给spinenr添加background是drawable的话,spinner自带的箭头会消失-->
<!--android:background="@drawable/spinner_shape"-->
<Spinner
android:id="@+id/spinner"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"/>
Adapter
您为微调框提供的选择可来自任何来源,但必须通过 SpinnerAdapter 来提供,例如,如果选择可通过数组获取,则通过 ArrayAdapter)来提供,如果选择可通过数据库查询获取,则通过 CursorAdapter 来提供。
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
simple_spinner_dropdown_item
与simple_spinner_item
的区别?
simple_spinner_dropdown_item是给列表用的,item有高度,是android:layout_height="?android:attr/dropdownListPreferredItemHeight"。
simple_spinner_item是普通的item,高度是wrap_content
spinner监听 AdapterView.OnItemSelectedListener()
刚打开这个activity,那么这个监听就获取数据了(一般是Array中的第一条数据),而不是必须要手动选择才会获取数。
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
String text = (String) adapterView.getItemAtPosition(position);
String text2 = (String) spinner.getSelectedItem();
Log.d(TAG, "setOnItemSelectedListener text=" + text);
Log.d(TAG, "setOnItemSelectedListener text2=" + text2);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
如何通过点击Button来获取Spinner里面选中的数据?
除了在监听中获取spinner选中的数据外,我们还可以这样:
String text = (String) spinner.getSelectedItem();
Demo
https://git.oschina.net/AndroidUI/SpinnerDemo01
问题
通过xml修改Spinner的指标图标和下拉list背景颜色
android:backgroundTint="@color/blue1"
android:popupBackground="@color/colorPrimary"