Android AutoCompleteTextView下拉列表的位置

19

我正在使用以下代码来实现自动完成,但是下拉菜单项出现在AutoCompleteTextView的上方。我希望它显示在AutoCompleteTextView的下方。我该怎么做?

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>

你能添加完整的布局 XML 吗? - Francesco verheye
现在编辑好了,你能看一下吗? - Figen Güngör
5个回答

13

看起来这一定会起作用:

android:dropDownHeight="100dp"

本文来源:https://dev59.com/8FnUa4cB1Zd3GeqPXymu#7648028

编辑:

我使用了一个新的活动并且下拉建议如下所示。如果您的marginTop为100dp,则可能需要将dropDownHeight更改为150dp。我猜测dropDownHeight必须高于Y位置(= padding + margin)。请告诉我它是否有效。


也许关键在于你必须让它变小以便能够在空间中显示出来。否则,它就会选择较大的那一个。 - Kimi Chiu

10

设置下拉菜单的高度和下拉菜单与编辑框之间的间距。下拉菜单将在dropdownanchor视图上方和下方显示。高度=200dp,下拉菜单不会覆盖键盘。

                <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>

9

在您的布局中使用AutoCompleteTextViewandroid:dropDownAnchorandroid:dropDownHorizontalOffsetandroid:dropDownVerticalOffset等属性。有关更多信息,请参见AutocompleteTextView页面。


3
android:dropDownVerticalOffset 就是我需要的东西。谢谢! - Mahmoud Elshamy
@MahmoudElshamy android:dropDownVerticalOffset 的确切用途是什么? - Ninja

4
让我们了解下拉视图的渲染方式。实际上,它是一个PopupWindow,通过方法findDropDownPosition决定位置(相对于视图锚点上方或下方)。该方法的注释如下:

在屏幕上定位弹出窗口。当弹出窗口太高无法适应锚点时,会查找并向上滚动父滚动视图以重新获取空间。如果无法滚动或滚动不足,则将弹出窗口移动到锚点上方。 定位结果放在{@code outParams}中。

我已经搜索了几天,但没有任何解决方案可以真正解决您想要处理下拉列表位置的问题(我的情况是在DialogFragment中)。这是我几天后的经验,你可以(欺骗)编写代码来解决此问题:
  • 使用android:dropDownAnchor设置下拉菜单的锚点,这个方法是可行的,但UI看起来有些奇怪
  • 再次使用setDropDownVerticalOffset(offset: Int),这个方法也是可行的,但UI还是有些奇怪
  • android:dropDownHeight设置为高于(上方或下方)空间的高度,结果与上述两种方式相同

3
添加下拉菜单高度和下拉菜单锚点,一定会起作用。
android:dropDownAnchor="@id/yourIdOfViewOnTop"
android:dropDownHeight="100dp"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接