Android layout padding is ignored by dropdown

前提是你 提交于 2020-01-13 19:44:28

问题


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

As you can see the

android:dropDownWidth="match_parent"

ignores the padding of the relative layout. Any ideas how to fix this?


回答1:


Solved it. I had to define another relative layout wrapping only the autocomplete textview and the button. This layout is the anchor of the popup, taking the width of the relative layout as its own width.The key:

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

Short version:

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



回答2:


//try this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">


    <AutoCompleteTextView
        android:id="@+id/autocomplete_city"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:hint="city_hint"
        android:inputType="text"
        android:dropDownWidth="match_parent"/>
    <Button
        android:id="@+id/locationButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="46dip"
        android:onClick="getLocation"
        android:text="icon_map_marker" />
</LinearLayout>


来源:https://stackoverflow.com/questions/19914328/android-layout-padding-is-ignored-by-dropdown

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!