首页 > 解决方案 > drawableLeft 仅在手机上未对齐 EditText

问题描述

在我的项目中,我把它们EditTexts放在一起。在IDE中,它看起来很好......

在此处输入图像描述

然而,在将其部署到我的手机后,EditText与可绘制对象未对齐

在此处输入图像描述

这仅在添加drawableLeft和时发生drawableStart。请注意,这 2 组EditTexts是在他们自己ConstraintLayout的水平LinearLayout上,并且仅在 和 的组合中drawableLeft发生LinearLayout

复制这个问题。只需创建一个新的布局资源并部署在您的手机上

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="2">

            <EditText
                android:id="@+id/txt"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:ems="10"
                android:inputType="number"
                android:maxLines="1"
                android:singleLine="true"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />

        </android.support.constraint.ConstraintLayout>

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2">

            <EditText
                android:id="@+id/txtDrawable"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:drawableLeft="@drawable/ic_android_black_24dp"
                android:drawablePadding="8dp"
                android:drawableStart="@drawable/ic_android_black_24dp"
                android:ems="10"
                android:inputType="number"
                android:maxLength="4"
                android:maxLines="1"
                android:singleLine="true"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent" />

        </android.support.constraint.ConstraintLayout>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

如何将EditText与可绘制的相邻对齐EditText

标签: androidandroid-layout

解决方案


问题是drawable的高度大于手机中的文本高度。线性布局将所有内容水平居中,这就是您遇到此对齐问题的原因。您在 IDE 或一部手机中看到的内容没有必要在其他设备中看到完全相同的内容。由于屏幕尺寸和分辨率,可能会发生这种情况。

解决方案可以是您可以将所有编辑文本包装在一个约束布局中。使可绘制的编辑文本位于中心,其余的编辑文本跟随它。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/layout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:id="@+id/txt"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:ems="10"
            android:inputType="number"
            android:maxLines="1"
            android:singleLine="true"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@id/txtDrawable"
            app:layout_constraintBottom_toBottomOf="@id/txtDrawable"/>

        <EditText
            android:id="@+id/txtDrawable"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:drawableLeft="@drawable/ic_android_black_24dp"
            android:drawablePadding="8dp"
            android:drawableStart="@drawable/ic_android_black_24dp"
            android:ems="10"
            android:inputType="number"
            android:maxLength="4"
            android:maxLines="1"
            android:singleLine="true"
            app:layout_constraintStart_toEndOf="@id/txt"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </android.support.constraint.ConstraintLayout>

</LinearLayout>


推荐阅读