这里主要介绍两种Spinner: 1.添加系统默认ArrayAdapter样式 2.采用之定义SpinnerAdapter样式 先看效果图:
首先看看自定义的SpinnerAdapter: 我门在这里借用下https://blog.csdn.net/wu_lai_314/article/details/24520249的定义
代码语言:javascript复制class SpinnerAdapter extends ArrayAdapter<String> {
Context context;
String[] items = new String[] {};
public SpinnerAdapter(final Context context,
final int textViewResourceId, final String[] objects) {
super(context, textViewResourceId, objects);
this.items = objects;
this.context = context;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(
android.R.layout.simple_spinner_item, parent, false);
}
TextView tv = (TextView) convertView
.findViewById(android.R.id.text1);
tv.setText(items[position]);
tv.setGravity(Gravity.CENTER);
tv.setTextColor(Color.BLUE);
tv.setTextSize(30);
return convertView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(
android.R.layout.simple_spinner_item, parent, false);
}
TextView tv = (TextView) convertView
.findViewById(android.R.id.text1);
tv.setText(items[position]);
tv.setGravity(Gravity.CENTER);
tv.setTextColor(Color.BLUE);
tv.setTextSize(30);
return convertView;
}
}
MainActivity很简单 就两个Spinner基本创建:
代码语言:javascript复制public class MainActivity extends Activity {
//存放系统自带版本adapter的Spinner
Spinner spinner_initially ;
//存放自定义adapter的Spinner
Spinner spinner_personal ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取界面布局文件中的Spinner组件
spinner_initially = (Spinner) findViewById(R.id.spinner_initially);
String[] arr_initially = new String[]{
"猫猫","狗狗","仓鼠"
};
//创建 ArrayAdapter对象
ArrayAdapter<String> adapter_initially = new ArrayAdapter<>(this,android.R.layout.simple_list_item_multiple_choice,arr_initially);
//为spinner设置adapter
spinner_initially.setAdapter(adapter_initially);
//获取界面布局文件中的Spinner组件
spinner_personal = (Spinner) findViewById(R.id.spinner_personal);
String[] arr_personaldesign = new String[]{
"猫猫","狗狗","仓鼠"
};
//创建 ArrayAdapter对象
SpinnerAdapter adapter_personal_design = new SpinnerAdapter(this,android.R.layout.simple_list_item_multiple_choice,arr_personaldesign);
//为spinner设置adapter
spinner_personal.setAdapter(adapter_personal_design);
}
}
然后就是布局,依然是无脑定义就好:
代码语言:javascript复制<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical" >
<!--定义了一个 Spinner组件-->
<!--指定组件的数组-->
<Spinner
android:id="@ id/spinner_initially"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/sex"
android:prompt="@string/sex_user_tip"
android:scrollbarSize="30sp"/>
<android.support.v7.widget.AppCompatSpinner
android:id="@ id/spinner_personal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="@string/sex_roommates_tip"
android:scrollbarSize="30sp"/>
</LinearLayout>
最后就是下拉选项的引用了 在values里建一个自定义xml就行 这里是/values/chooseArrays
代码语言:javascript复制<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="sex">
<item>男孩</item>
<item>女孩</item>
<item>荷兰猪</item>
</string-array>
</resources>
因为采用默认ArrayAdapter由很多限制 掌握了自定义的方法, 只要设计能力够强就可以做出各种酷炫界面