首先在xml里面加一个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"
tools:context="${packageName}.${activityClass}" >
<Spinner
android:id="@+id/sp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
然后再加一个Spinner的item的xml,里面设置每一列的效果,名字叫:spitem.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="40sp"
android:textSize="30sp"
android:background="#FFFFFF"
android:singleLine="true" />
最后在代码中加载Spinner
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=(Spinner)findViewById(R.id.sp);
String data[]=new String[]{"a","b","c"};
//声明一个ArrayAdaper,用来装载Spinner数据
ArrayAdapter<String>adapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, data);
//加载item的布局样式
adapter.setDropDownViewResource(R.layout.spitem);
//最后让Spinner加载adapter
spinner.setAdapter(adapter);
}
}
转载于:https://blog.51cto.com/luoguoxin/1606515