在我的项目中,我将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
的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>