下拉菜单忽略了Android布局填充

标签 android android-layout android-listview

enter image description here

作为 xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="20dp">

    <Button
        android:id="@+id/locationButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:minWidth="46dip"
        android:onClick="getLocation"
        android:text="@string/icon_map_marker" />

    <AutoCompleteTextView
        android:id="@+id/autocomplete_city"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:hint="@string/city_hint"
        android:inputType="text"
        android:layout_alignBottom="@+id/locationButton"
        android:layout_alignTop="@id/locationButton"
        android:layout_toLeftOf="@id/locationButton"
        android:dropDownWidth="match_parent"
        />

    <requestFocus />

</RelativeLayout>

如你所见

android:dropDownWidth="match_parent"

忽略相对布局的填充。有什么解决办法吗?

最佳答案

解决了。我必须定义另一个只包含自动完成 TextView 和按钮的相对布局。这个布局就是弹窗的 anchor ,把相对布局的宽度作为自己的宽度。关键:

android:dropDownWidth="wrap_content"
android:dropDownAnchor="@+id/anchor"

简短版:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="20dp" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/anchor" >

        <Button
            android:id="@+id/locationButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" />

        <AutoCompleteTextView
            android:id="@+id/autocomplete_city"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/locationButton"
            android:layout_alignTop="@id/locationButton"
            android:layout_centerHorizontal="true"
            android:layout_toLeftOf="@id/locationButton"
            android:dropDownWidth="wrap_content"
            android:hint="@string/city_hint"
            android:dropDownAnchor="@+id/anchor" />

        <requestFocus />

    </RelativeLayout>

</RelativeLayout>

关于下拉菜单忽略了Android布局填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19914328/

相关文章:

android - READ_PRIVILEGED_PHONE_STATE 权限错误

android - 我如何设计一个类似 "family tree"的用户界面?

android - 从自定义 ListView 类中刷新 ListView

android - ListView 中的字母索引..忽略部分索引

android - Google Play 说我的应用程序由于 list 原因与平板电脑的某些运营商型号不兼容。什么?

android - 我的android模拟器不工作,请帮忙?

android - 状态通知的默认 XML

android - 如何根据屏幕增加表格行的高度?

java - ListView 中的图像混合在一起

android - 动态设置 9patch 背景