在 Android 中并排对齐 TextView 和 AutoCompleteTextView

Aligning TextView and AutoCompleteTextView side by side in Android

我正在尝试将以下 AutoCompleteTextView(类型组位置..)和 TextView(Radius)向左对齐,关于顶部 ImageView。

如您所见,它不起作用。 这是我的 xml 布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="0dp">


<ImageView
        android:id="@+id/icon"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"/>

    <AutoCompleteTextView
        android:id="@+id/autocomplete_textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/icon"
        android:layout_below="@+id/icon"
        android:paddingBottom="0dp"
        android:inputType="textAutoComplete"
        android:textSize="16sp" />
    
    <TextView
        android:id="@+id/radiusText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/autocomplete_textview"
        android:layout_alignLeft="@+id/icon"
        android:textSize="16sp"
        android:paddingTop="5dp"/>
    
</RelativeLayout>

我已经尝试但失败的:

根据 AutoCompleteTextView 对齐 TextView,并根据父视图对齐两个视图。

我怀疑 AutoCompleteTextView 有一些透明尾随,或者我遗漏了什么。

将下行添加到您的 AutoCompleteTextView 和 TextView

  android:layout_marginLeft="30dp"

如果你想要这样:

然后使用垂直线性布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="20dp">


<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="20dp"
    android:id="@+id/imageView5" 
    android:src="@drawable/ic_setting_light"/>

<AutoCompleteTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:hint="Location here"/>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:hint="Radius(in meters)"/>

</LinearLayout>

或者如果你想这样:

使用带权重的水平 LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3"
android:layout_marginTop="20dp">


<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="0.5">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_setting_light"
        android:layout_centerInParent="true"/>

</RelativeLayout>
<RelativeLayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1.5">
     <AutoCompleteTextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:hint="Location"
     android:layout_centerInParent="true"
     />
</RelativeLayout>


<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0">
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Radius (m)"
        android:layout_centerInParent="true"
         />
</RelativeLayout>
</LinearLayout>

将图片替换为您的家并提供所需的 ID。 希望能帮助到你。 谢谢。

至少找到了我的解决方案。

出于某种原因,AutoCompleteTextView 具有默认填充,一旦我将 paddingLeft 设置为 0,它就解决了问题。 :)