主要参考博客Android 实现自定义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 --Spinner--自定义Spinner的更多相关文章

  1. Android 的自定义Spinner组件实现方式

    一.Android的API方式默认实现的方式 1.layout下编辑main_activity.xml <RelativeLayout xmlns:android="http://sc ...

  2. Android UI自定义Spinner下拉框(用popuwindow实现)-转

    定义出第一个图片的布局和弹出框(一个listView)的布局,,这里就不在多说了~ListView需要自己定义一个MyspinnerAdapter~做好这些准备之后,就是弹出框的实现了~  prote ...

  3. [置顶] xamarin android自定义spinner

    以前弄的一个下拉框时自带的spinner,感觉好丑,实际效果实在满足不了基本的UI界面要求,还是自己动手丰衣足食,看了网上关于android中自定义spinner的文章,感觉实现原理还是比较简单,所以 ...

  4. Android 自定义Spinner和其下拉窗口

    : 自定义Spinner其实包括两个部分: 第一部分是用来打开下拉列表的按钮,如图,这个绿色背景直接设置Spinner的背景就行,素材文件如下: 里面的文字需要注意下,Spinner控件没有直接修改文 ...

  5. (转)Android 自定义 spinner (背景、字体颜色)

    Android 自定义 spinner (背景.字体颜色) (2012-07-04 17:04:44)   1.准备两张图片,并做好9.png   2.在drawable中定义spinner_sele ...

  6. Android 自定义 spinner (背景、字体颜色)

    转自:http://blog.sina.com.cn/s/blog_3e333c4a010151cj.html 1.准备两张图片,并做好9.png   2.在drawable中定义spinner_se ...

  7. spinner自定义,效果如腾讯QQ账号选择时候的下拉列表

         下拉列表在android中自带spinner的有时候不太适合我们的界面,我们希望有自己的一种显示方法,那怎么办?自定义Spinner.效果如QQ账号选择一样.如图所以. 这种效果,如果你喜欢 ...

  8. [Android实例] 有关spinner 的item问题 谁能给解答下??

    [Android实例] 有关spinner 的item问题 (更多Android问题解决,Android开发讨论 请访问:http://www.eoeandroid.com/forum.php)

  9. 一个背景图实现自定义spinner样式

    如下界面:由一个spinner两个EditText一个Button实现,为了保持界面的统一性,需要把默认的spinner样式改成类似下面的样式. xml文件布局如下图 这里用一个LinerLayout ...

随机推荐

  1. How to: Fix a network printer suddenly showing as offline in Windows Vista, 7 or 8 « Robin's Blog

    This post has become quite popular – so I've updated it with a bit more detail, plus some people's e ...

  2. jQuery前端验证多种方式

    JQuery Validate使用总结:一.导入js库<script src="../js/jquery.js" type="text/javascript&quo ...

  3. Hadoop2 实战系列之1 -- Hortonworks Sandbox的安装和使用

    欢迎转载,转载请注明出处,谢谢,徽沪一郎. 概要 本文主要讲述如何利用hortonworks sanbox来搭建hadoop2的学习环境.Hortonworks sanbox集成了hadoop2及其上 ...

  4. PHP学习(一)----变量及字符串

    PHP中的变量: 1. 定义:$符号来定义变量 2. 说明: (1)PHP弱语言,定义变量的时候不用声明类型,但是并不代表PHP没有数据类型 (2)变量名是区分大小写的,只能是数字,字母或者下划线 ( ...

  5. General protection fault Exceptions in Linux/IA32 Systems

    Computer Systems A Programmer's Perspective Second Edition Exception number Description Exception cl ...

  6. JS中基本window对象操作

    ---恢复内容开始--- 一.使用window中的属性时   window.属性,直接跟属性名.而调用window的函数时  window.hanshu(): 要在其函数名后面加括号. 二.windo ...

  7. 【mysql的紧急应用】

    1. 字符串替换 今天老板说要将商品表title中的"AAA"全部改成"BBB",于是乎,百度得到答案. UPDATE goods set title=REPL ...

  8. BAE3.0上的java+tomcat代码发布

    ---------------------------------2016/01/25更新-------------------------------------- 最近两天去百度开放云,发现它再也 ...

  9. nodejs express测试

    1.页面请求 app.get('/list_user', function (req, res) { console.log("/list_user GET 请求"); //res ...

  10. style="display"之后不能获取offsetHeight或clientWidth这类测量的值

    如果在html元素中设置了style="display:none;height:90px;"的属性后,是无法获得offsetLeft ,offsetWidth,offsetTop, ...