如何实现Android AutoCompleteTextView下拉位置?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (400)

以下是我的代码:

package com.example.autocomplete;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.activity_main);

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_list_item_1, COUNTRIES);
         AutoCompleteTextView textView = (AutoCompleteTextView)
                 findViewById(R.id.countries_list);
         textView.setAdapter(adapter);
     }

     private static final String[] COUNTRIES = new String[] {
         "Belgium","Belgam","Bellam", "France Belgium", "Italy", "Germany", "Spain"
     };
 }

XML:

<RelativeLayout 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"
    tools:context="${relativePackage}.${activityClass}" >

    <AutoCompleteTextView
        android:id="@+id/countries_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:dropDownHeight="100dp"
        android:ems="10"
        android:text="AutoCompleteTextView" >

        <requestFocus />
    </AutoCompleteTextView>

</RelativeLayout>
提问于
用户回答回答于
用户回答回答于

设置下拉列表的高度,并在下拉文本和编辑文本之间设置空间

                <RelativeLayout
                    android:id="@+id/dropdownAutoCompleteTextView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingBottom="8dp"
                    android:paddingTop="8dp">
                    <AutoCompleteTextView
                        android:id="@+id/autoTextViewState"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:digits="@string/get_started_petname_digits"
                        android:dropDownAnchor="@+id/dropdownAutoCompleteTextView"
                        android:dropDownHeight="200dp"
                        android:hint="Colorado"
                        android:imeOptions="actionNext"
                        android:inputType="textEmailAddress|textCapWords"
                        android:maxLength="15"
                        android:nextFocusDown="@+id/editZipCode"/>
                </RelativeLayout>

扫码关注云+社区

领取腾讯云代金券