Android --Spinner--自定义Spinner
2015-09-11 16:32
621 查看
主要参考博客Android 实现自定义Spinner
1、Spinner样式
2、需要使用的ListView的Item的样式
3、ListView的Adapter,这里是使用了两个Layout来配置Adapter
4、自定义Dialog
5、自定义的Spinner
6、Activity中使用Spinner
1、Spinner样式
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#defafd" > <ListView android:id="@+id/spinner_search_expand_list" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:cacheColorHint="@null" android:scrollbars="none" android:divider="@android:drawable/divider_horizontal_bright"/> </LinearLayout>
2、需要使用的ListView的Item的样式
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" android:padding="10dp"> <TextView android:id="@+id/search_expand_item_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:visibility="gone"/> <TextView android:id="@+id/search_expand_item" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center_vertical" android:padding="10dp" android:layout_weight="1" android:textSize="18sp" /> </LinearLayout>
3、ListView的Adapter,这里是使用了两个Layout来配置Adapter
public View getView(int position, View view, ViewGroup viewGroup) { SearchConditionListItem item = getItem(position); if (view == null) { view = View.inflate(mcontext, layout, null); searchConditionTxt = (TextView) view.findViewById(R.id.search_expand_item); searchConditionTxtId=(TextView)view.findViewById(R.id.search_expand_item_id); //searchConditionItemCheck=(CheckBox)view.findViewById(R.id.search_expand_item_check); searchConditionDisplay=(TextView)view.findViewById(android.R.id.text1); } if(searchConditionTxt != null) { searchConditionTxt.setText(item.getExpandName()); } if(searchConditionTxtId != null) { searchConditionTxtId.setText(item.getExpandid()); } if(searchConditionDisplay != null){ searchConditionDisplay.setText(item.getExpandName()); } return view; }
4、自定义Dialog
package com.dr.app.drseamoniter.dialog; import android.app.AlertDialog; import android.content.Context; import android.os.Bundle; /** * Created by Administrator on 15-9-11. */ public class SearchSelectDialog extends AlertDialog{ public SearchSelectDialog(Context context, int theme) { super(context, theme); } public SearchSelectDialog(Context context) { super(context); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } }
5、自定义的Spinner
@Override public boolean performClick() { Context context = getContext(); final LayoutInflater inflater = LayoutInflater.from(getContext()); //弹出框视图 final View view = inflater.inflate(R.layout.spinner_search_condition, null); //视图中的List final ListView listview = (ListView) view.findViewById(R.id.spinner_search_expand_list); // btnOK = (Button) view.findViewById(R.id.search_condition_btnOK); // btnCancel = (Button) view.findViewById(R.id.search_condition_btnCancel); // // btnOK.setOnClickListener(new MyOKCancelClickListener()); // btnCancel.setOnClickListener(new MyOKCancelClickListener()); //根据ArrayList<>数据源 确定Adapter adapters = new SearchConditionListAdapter(context, getList()); //listview绑定Adapter listview.setAdapter(adapters); //监听ItemClick listview.setOnItemClickListener(this); //设置dialog dialog = new SearchSelectDialog(context, R.style.dialog);//创建Dialog并设置样式主题 LayoutParams params = new LayoutParams(650, LayoutParams.FILL_PARENT); dialog.setCanceledOnTouchOutside(true);// 设置点击Dialog外部任意区域关闭Dialog dialog.show(); dialog.addContentView(view, params); return true; }
6、Activity中使用Spinner
private void setAreaConditionSpinner(){ mareaExpandList =new ArrayList<SearchConditionListItem>(); SearchConditionListItem item =new SearchConditionListItem("1",牧场"); mareaExpandList.add(item); item=new SearchConditionListItem("2","陆地"); mareaExpandList.add(item); mareaCondition.setList(mareaExpandList); mareaConditionAdapter = new SearchConditionListAdapter(this , mareaExpandList,android.R.layout.simple_spinner_item); mareaCondition.setAdapter(mareaConditionAdapter); mareaCondition.setOnItemSelectedListener(new MyAreaSpinnerItemSelectedListener()); mareaCondition.setPrompt("请选择区域"); } private void setDevConditionSpinner(String areaid){ mdevExpandList = new ArrayList<SearchConditionListItem>(); SearchConditionListItem item = new SearchConditionListItem("11","底层"); mdevExpandList.add(item); item=new SearchConditionListItem("12","底层2"); mdevExpandList.add(item); mdevExpandList.add(item); mdevCondition.setList(mdevExpandList); mdevConditionAdapter = new SearchConditionListAdapter(this , mdevExpandList,android.R.layout.simple_spinner_item); mdevCondition.setAdapter(mdevConditionAdapter); mareaCondition.setPrompt("请选择设备"); }
相关文章推荐
- Android Handler机制
- Android测试AsyncTask下载图片
- Android Studio 导入 jar 和 .so 的方法
- Android 根据一个传入的String选择spinner里面的默认值
- ADT对应的.android中找不到adb_usb.ini
- Android——systrace使用分析
- android EditText控件事件监听
- Android 实时滤镜 高斯模糊
- 二、Android应用的界面编程(二)布局管理器
- 清除android程序中的多余资源
- android 弹出选择框
- FragmentTabHost 自定义
- android项目迁移到新android studio所遇到的Gradle问题汇总
- android context的用法和遇到的问题
- Android studio jni
- Android - 百度地图打包之后出现的问题
- Android(java)学习笔记230:服务(service)之绑定服务的细节
- Android系统启动过程
- Android Skia和2D图形系统
- android中的view动画