1. 實現最簡單的spinner
xml文件,有一個TextView,一個Spinner:
<RelativeLayout 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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="50dp" />
</RelativeLayout>
.java文件
public class MainActivity extends ActionBarActivity {
private static final String[] name={"劉備","關羽","張飛","曹操","小喬"};
private TextView text ;
private Spinner spinner;
private ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text = (TextView) findViewById(R.id.textView);
spinner = (Spinner) findViewById(R.id.spinner);
//將可選內容與ArrayAdapter連接起來,simple_spinner_item是android系統自帶樣式
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,name);
//設置下拉列表的風格,simple_spinner_dropdown_item是android系統自帶的樣式,等會自己定義改動
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//將adapter 加入到spinner中
spinner.setAdapter(adapter);
//加入事件Spinner事件監聽
spinner.setOnItemSelectedListener(new SpinnerSelectedListener());
}
//使用數組形式操作
class SpinnerSelectedListener implements AdapterView.OnItemSelectedListener {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
text.setText("我的名字是:"+name[arg2]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
執行效果:
—————————————————————
使用xml文件作為數據源創建adapter:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="songs">
<item>沒有人</item>
<item>我的快樂時代</item>
<item>黃金時代</item>
<item>習慣失戀</item>
<item>你來自哪顆星</item>
</string-array>
</resources>
.java文件:
public class SpinnerActivity extends Activity {
private TextView text;
private Spinner spinner;
private ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner);
spinner = (Spinner) findViewById(R.id.spinner);
text = (TextView) findViewById(R.id.textView);
//將可選內容與ArrayAdapter連接起來
adapter = ArrayAdapter.createFromResource(this, R.array.songs, android.R.layout.simple_spinner_item);
//設置下拉列表的風格
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//將adapter2 加入到spinner中
spinner.setAdapter(adapter);
//加入事件Spinner事件監聽
spinner.setOnItemSelectedListener(new SpinnerXMLSelectedListener());
}
//使用XML形式操作
class SpinnerXMLSelectedListener implements OnItemSelectedListener{
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,
long arg3) {
text.setText("你使用什么樣的手機:"+adapter.getItem(position));
}
public void onNothingSelected(AdapterView<?
> arg0) { } } }
spinner有三個屬性能夠記一下:
android:spinnerMode="dropdown"
android:dropDownVerticalOffset="-50dp"
android:dropDownHorizontalOffset="20dp"
android:popupBackground="#f0000000"
spinnerMode=dropdown時,為下拉模式
spinnerMode=dialog時,會在界面中間彈出
android:popupBackground=”#f0000000”,能夠去除spinner的默認黑邊
dropDownVerticalOffset和dropDownHorizontalOffset都是改變下拉框位置的
2.自己定義spinner樣式
改變字體顏色、大小和背景:
新建一個xml布局文件,命名為spinner_item.xml:
<?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="wrap_content" android:paddingRight="5dp" android:textColor="#f77718" android:gravity="left" android:textSize="15sp" android:padding="10dp" android:singleLine="true" android:text="New Text" android:id="@+id/textView32" />
再創建一個下拉框樣式布局的xml文件。命名為dropdown_stytle.xml:
<?
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="match_parent" android:text="New Text" android:padding="10dp" android:singleLine="true" android:textSize="15sp" android:textColor="#f77718" android:gravity="left" android:background="#aa33ac" android:id="@+id/textView3333" />
改動之前.java中的
為:
adapter = new ArrayAdapter<String>(this,R.layout.spinner_item,name);
adapter.setDropDownViewResource(R.layout.dropdown_style);
假設下拉框有黑邊,能夠在spinner中加上屬性android:popupBackground=”#f0000000”,能夠去除spinner的默認黑邊,that’s all~
最后的效果圖: