首页 > 解决方案 > 为什么 layout_weight 在 TextInputLayout 的孩子中不起作用?

问题描述

我有以下内容:

   <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:alpha=".9"
        android:background="@color/light_gray_search"
        android:padding="16dp">

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="1"
        app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:importantForAccessibility="no"
            app:srcCompat="@drawable/myImage" />

        <AutoCompleteTextView
            android:id="@+id/myAutoCompleteTextView"
            android:layout_weight="0.8"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/myAutoHint"
            android:imeOptions="actionNext"
            android:inputType="textPersonName"
            android:maxLines="1"/>

    </com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

知道TextInputLayout extends LinearLayout,并且我使用LinearLayoutxml属性(weightSum,,, orientationlayout_weight首先对我的imageView进行水平对齐,然后是autoCompleteText,

为什么这不起作用?

当前结果:即使在分配layout_weight.

标签: androidxml

解决方案


实际上找到了答案,将其发布在这里以供将来参考:

添加一个LinearLayout, 并分别拥有LinearLayoutbeImageViewTextInputLayouton 水平方向的子级。

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
android:orientation="horizontal"
android:weightSum="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent">

<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:importantForAccessibility="no"
app:srcCompat="@drawable/myImage"
android:layout_gravity="center_vertical"/>

<com.google.android.material.textfield.TextInputLayout
..  
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.9"
..>  

<AutoCompleteTextView
../>

</com.google.android.material.textfield.TextInputLayout>

</LinearLayout>

推荐阅读