首页 > 解决方案 > 复选框和 TextInputLayout 在线性布局内未正确对齐

问题描述

我在衬里布局中有两个复选框和一个 TextInputLayout。但他们没有得到正确渲染。复选框正在向下移动。我尝试了一切,但它们没有对齐。

我的代码如下:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="horizontal">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/layoutSysCardNo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_weight="2"
            app:boxBackgroundColor="@color/white_50"
            app:counterMaxLength="10">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/sysCardNo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/field_enter_sys_job_no"
                android:inputType="number" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.checkbox.MaterialCheckBox
            android:id="@+id/hmsiWarranty"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_weight="1"
            android:text="@string/vehicle_delievered" />

        <com.google.android.material.checkbox.MaterialCheckBox
            android:id="@+id/extWarranty"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_weight="1"
            android:text="@string/vehicle_delievered" />

    </LinearLayout>

输出如下

在此处输入图像描述

标签: androidandroid-layout

解决方案


只需 android:gravity="center"在你的LinearLayout它会解决你的问题

尝试这个

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="center"
        android:orientation="horizontal">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/layoutSysCardNo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_weight="2"
            app:boxBackgroundColor="@color/white_50"
            app:counterMaxLength="10">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/sysCardNo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/field_enter_sys_job_no"
                android:inputType="number" />
        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.checkbox.MaterialCheckBox
            android:id="@+id/hmsiWarranty"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_weight="1"
            android:text="@string/vehicle_delievered" />

        <com.google.android.material.checkbox.MaterialCheckBox
            android:id="@+id/extWarranty"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_weight="1"
            android:text="@string/vehicle_delievered" />

    </LinearLayout>

推荐阅读