---
title: "Android中用Spinner和AutoCompleteTextView产生类似PC或网站上的下拉框列表"
author: "hanyezhiyu8"
date: "2011-09-18T03:43:41Z"
tags: []
categories: ["移动开发"]
slug: "android-spinner-autocompletetextview-pc"
draft: false
lastmod: "2011-09-18T04:08:38Z"
id: 1174321
url: "https://inuts.iteye.com/blog/1174321"
---

**原创链接：**[http://inuts.iteye.com/blog/1174321](/blog/1174321) **转载请注明**

Android原生自带的下拉框实在不太好看，只想产生一个类似于PC或网站上的那种自动完成类型的下拉框。  
用到的控件为Spinner和AutoCompleteTextView。

在java文件中：

 

```
        String[] mListItems = {"item1", "item2", "item3"};        
        ArrayAdapter<String> mArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, mListItems); 
        final Spinner mSpinner = (Spinner) findViewById(R.id.type_Spinner);
		mSpinner.setAdapter(mArrayAdapter);
		mSpinner.setSelection(0);
		
		final AutoCompleteTextView mAutoCompleteTextView = (AutoCompleteTextView)this.findViewById(R.id.type); 
        mAutoCompleteTextView.setAdapter(mArrayAdapter); 
        mAutoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub
				mSpinner.setSelection(position);
			}
        });
        
        FrameLayout layout = (FrameLayout)findViewById(R.id.type_clicklayout);
        layout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mAutoCompleteTextView.showDropDown();
			}
        });
```

 

在xml文件中：

 

```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">      
	<FrameLayout
		android:layout_width="fill_parent" 
		android:layout_height="50dip"
		android:layout_weight="1">		
		<LinearLayout android:orientation="horizontal"
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:gravity="center">
		<AutoCompleteTextView
		    android:id="@+id/type"
		    android:layout_width="fill_parent"
		    android:layout_height="50dip"
		    android:gravity="center"
		    android:focusable="false"
	        android:background="@null"
	        android:cacheColorHint="#00000000"/>
		</LinearLayout>
		
		<LinearLayout android:orientation="horizontal"
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:gravity="center">
		<Spinner
			android:id="@+id/type_Spinner"
			android:layout_width="fill_parent"
			android:layout_height="50dip"
			android:gravity="center"					
			android:clickable="false"/>
		</LinearLayout>
		
		<LinearLayout android:orientation="horizontal"
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:gravity="center">
		<FrameLayout
			android:id="@+id/type_clicklayout"
			android:layout_width="fill_parent" 
			android:layout_height="50dip"
			android:clickable="true"/>
		</LinearLayout>	
	</FrameLayout>
</LinearLayout>
```

效果图(2.2)：

![](http://dl.iteye.com/upload/attachment/555970/22d9e1c5-6207-3928-91cd-4dc7827cd7b0.png)

效果图(2.3)：

![](http://dl.iteye.com/upload/attachment/555972/2ceb1891-2317-3efe-bbfc-fa496d72b523.png)

**原创链接：**[http://inuts.iteye.com/blog/1174321](/blog/1174321) **转载请注明**