如何防止AutoCompleteTextView下拉框覆盖AutoCompleteTextView?

5

我有一个 AppCompatAutoCompleteTextView,其锚点比 AppCompatAutoCompleteTextView 还要低。这是设计上的问题,无法更改。当我在下拉列表上滚动时,下拉列表会向上移动并覆盖 AppCompatAutoCompleteTextView

enter image description here

这是正确的工作方式吗?

这是我的代码:

Main activity 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.autocompletetest.MainActivity">
    <android.support.v7.widget.AppCompatAutoCompleteTextView
        android:id="@+id/autocomplete"
        android:layout_width="match_parent"
        android:layout_height="32dp"

        android:layout_gravity="center"
        android:layout_marginLeft="8dp"
        android:layout_weight="1"
        android:alpha="0.87"
        android:background="#ffffff"
        android:dropDownWidth="match_parent"
        android:dropDownAnchor="@+id/anchor"
        android:gravity="center_vertical"

        android:hint="entervalue"
        android:inputType="textUri"
        android:popupBackground="@null"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:textColor="#000000"
        android:textSize="14sp"
        android:layout_alignParentTop="true"
        android:textColorHint="#000000"
        />
    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:layout_marginTop="3dp"
        android:layout_below="@+id/autocomplete"
        android:id="@+id/anchor"
        android:background="#aaaaaa"/>
</RelativeLayout>

主活动Java:

AppCompatAutoCompleteTextView auto = (AppCompatAutoCompleteTextView) findViewById(R.id.autocomplete);
        String[] countries = getResources().getStringArray(R.array.colors);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,countries);
        auto.setAdapter(adapter);

编辑:我应该提到,我在Marshmallow上看到了这种行为。

3个回答

3
这个问题似乎是由于 android:popupBackground="@null" 引起的。

3
从AutoCompleteTextView中删除--> android:popupBackground="@null",然后检查它。

是的,这就是答案所说的。 - casolorz

2

根据文档,它说:

将自动完成下拉菜单锚定到的视图。 如果未指定,则使用文本视图本身。

你需要自定义锚点吗?我想默认情况下可能由于相对布局而没有此行为。


不幸的是,设计要求下拉菜单比AutoCompleteTextView所在位置稍微向下一点。 - casolorz
我会改用垂直方向的LinearLayout。我认为在这里没有必要使用RelativeLayout。 - egfconnor
相同的问题也会在LinearLayout中发生。另外,如果我让下拉菜单显示在锚点上方,那么在工具栏和状态栏上滚动时也会出现问题。实际上,这个问题是由于android:popupBackground="@null"引起的。 - casolorz

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