本篇构造spiner使用了两种方式,一种是默认的视图一种是自定义的视图
这里放两张效果图
因为这是项目直接截图,可能有些没用的代码,大家看有用的吧
1:默认的模式
这是activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"这是MainActivity
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity"><Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content"> </Spinner>
</LinearLayout>
package com.centaur.uiset;那么能简单就能实现一个默认的视图效果import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;import java.util.ArrayList;
import java.util.List;public class MainActivity extends Activity {
private Spinner mspinner;
private String[] mDatas;
private List<SpinnerItemData> mDatasList= new ArrayList<SpinnerItemData>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mspinner = (Spinner) findViewById(R.id.spinner);
initSpinner();
}
private void initSpinner() {
//建立数据源
mDatas = getResources().getStringArray(R.array.label);
//构建适配器
ArrayAdapter<String> madapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mDatas);
//绑定适配器
mspinner.setAdapter(madapter);
}
}
2:自定义视图,我这里用用了一个图像和一个行文字
这是我的自定义视图spinneritem.xml
</pre><pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>在这里我构造了一个SpinnerAdapter 继承了BaseAdapter
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:background="#ff6b6b6b"
android:layout_height="1px"></LinearLayout>
<LinearLayout
android:paddingRight="13dp"
android:paddingLeft="40dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/spinnerimageview"
android:layout_width="40dp"
android:layout_height="40dp" />
<TextView
android:id="@+id/spinnertextview"
android:text="编程"
android:textSize="20sp"
android:layout_marginLeft="40dp"
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" /></LinearLayout>
</LinearLayout>
这里这个写的还算规范,欢迎大家指正
package com.centaur.uiset;import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;import java.util.List;
/**
-
Created by Tianchaoxiong on 2015/5/10.
*/
public class SpinnerAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<SpinnerItemData> mdatas;
private Context mContext;
public SpinnerAdapter(Context mContext,List<SpinnerItemData> mdatas){
this.mInflater = LayoutInflater.from(mContext);
this.mContext =mContext;
this.mdatas = mdatas;
}
@Override
public int getCount() {
return mdatas.size();
}@Override
public Object getItem(int position) {
return mdatas.get(position);
}@Override
public long getItemId(int position) {
return position;
}@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
///View view = null;
SpinnerItemData spinnerItemData = mdatas.get(position);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.spinneritem, null);holder = new ViewHolder(); /*得到各个控件的对象*/ holder.mImageView = (ImageView) convertView.findViewById(R.id.spinnerimageview); holder.mTextView = (TextView) convertView.findViewById(R.id.spinnertextview); convertView.setTag(holder);//绑定ViewHolder对象 } else { holder = (ViewHolder) convertView.getTag();//取出ViewHolder对象 } /*设置TextView显示的内容,即我们存放在动态数组中的数据*/ holder.mImageView.setImageResource(spinnerItemData.getImage()); holder.mTextView.setText(spinnerItemData.getLebel()); return convertView;
}
/存放控件/
public final class ViewHolder{
public ImageView mImageView;
public TextView mTextView;
}
}
最后就是主视图了
package com.centaur.uiset;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity {
private Spinner mspinner;
private String[] mDatas;
private List<SpinnerItemData> mDatasList= new ArrayList<SpinnerItemData>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mspinner = (Spinner) findViewById(R.id.spinner);
//建立数据源
initDatas();
//构建适配器
SpinnerAdapter adapter = new SpinnerAdapter(MainActivity.this,mDatasList);
//绑定适配器
mspinner.setAdapter(adapter);
//initSpinner();
}
private void initDatas() {
//拿到text内容
mDatas = getResources().getStringArray(R.array.label);
//拿到image内容
for(int i = 0; i<26;i++){
int tmp = i+'a';
String str = "label"+(char)tmp;
int imageId = getResources().getIdentifier(str, "raw", getPackageName());
SpinnerItemData spinnerItemData = new SpinnerItemData(imageId,mDatas[i]);
mDatasList.add(spinnerItemData);
}
}
private void initSpinner() {
//建立数据源
mDatas = getResources().getStringArray(R.array.label);
//构建适配器
ArrayAdapter<String> madapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mDatas);
//绑定适配器
mspinner.setAdapter(madapter);
}
}
欢迎指正。。。