Spinner控件详解
效果图
修改Spinner样式
6.x & 5.x系统样式
4.x系统样式
官方文档
android:dropDownHorizontalOffset | setDropDownHorizontalOffset(int) | Amount of pixels by which the drop down should be offset horizontally. |
android:dropDownSelector | List selector to use for spinnerMode=”dropdown” display. | |
android:dropDownVerticalOffset | setDropDownVerticalOffset(int) | Amount of pixels by which the drop down should be offset vertically. |
android:dropDownWidth | setDropDownWidth(int) | Width of the dropdown in spinnerMode=”dropdown”. |
android:gravity | setGravity(int) | Gravity setting for positioning the currently selected item. |
android:popupBackground | setPopupBackgroundResource(int) | Background drawable to use for the dropdown in spinnerMode=”dropdown”. |
android:prompt | The prompt to display when the spinner’s dialog is shown. | |
android:spinnerMode | Display mode for spinner options. |
填充数据
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// 建立数据源
String[] mItems = {"Item 1", "Item 2", "Item 3", "Item 4"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
点击监听
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// TODO
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO
}
});
设置下拉框的位置(偏移量)
设置垂直偏移量
- android:dropDownVerticalOffset
- setDropDownVerticalOffset(int)
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_margin="20dp"
android:dropDownVerticalOffset="40dp" />
设置水平偏移量
- android:dropDownHorizontalOffset
- setDropDownHorizontalOffset(int)
呃……
设置下拉框背景色
- android:popupBackground
- setPopupBackgroundResource(int)
android:popupBackground="@drawable/shape_green"
自定义Spinner内文字字体样式
方式一
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, mItems);
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#FF5A5A5A"
android:ellipsize="marquee"
android:textAlignment="inherit"/>
方式二
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// 获取到TextView
TextView tv = (TextView)view;
// 修改样式属性
tv.setTextColor(Color.BLUE);
tv.setTextSize(15f);
tv.setGravity(Gravity.CENTER);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
Toast.makeText(MainActivity.this, "Nothing", Toast.LENGTH_SHORT).show();
}
});
自定义下三角样式
方式一
方式二
<Spinner
……
android:background="@android:color/transparent"
…… />
android:drawableRight="@mipmap/dropmenu_icon_down"
自定义下拉框中Item样式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
<?xml version="1.0" encoding="utf-8"?><!--
/*
**Spinner 下拉Item的样式
*/
-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="@dimen/y61"
android:ellipsize="marquee"
android:gravity="right|center"
android:maxEms="30"
android:paddingRight="@dimen/x28"
android:singleLine="true"
android:textColor="#FF262626"
android:textSize="@dimen/x28" />
设置下拉框风格
- android:spinnerMode
dropdown
android:spinnerMode="dropdown"
dialog
android:spinnerMode="dialog"
设置下拉框宽度
- android:dropDownWidth
其他
android:dropDownSelector
android:prompt