android - 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
?
解决方案
问题是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>
推荐阅读
- security - 如何从 FTP 服务器获取文件?
- prolog - 在 sicstus prolog 中将字符串转换为列表
- excel - 如何自动跨工作表粘贴?
- json - XML to JSON:转换为 JSON 后如何重构节点顺序
- vue.js - Nuxt.js - npm generate 后,外部组件在另一个组件中不起作用
- node.js - 如何在无需全局安装 ts-node 或 npx 的情况下完全运行 nodemon + ts-node + typescript?
- c++ - C ++:如何初始化包含具有给定键的指针列表的映射?
- azure-pipelines - 我们是否需要设置“isOutput=true”以将变量用作同一作业中任务的任务输入?
- huffman-code - 霍夫曼编码中如何存储频率表?
- javascript - 单击时的 Jquery 返回一个值并在另一个函数中使用它