如何不重叠 EditText 中按钮上的文本?
How to not overlap the text on buttons in EditText?
因此,在我的密码字段中,我尝试将这些图标添加为按钮。现在我面临的问题是文本在该按钮上重叠。实际上我想要的是按钮保留在 edittext 中,但文本不会重叠,但您仍然应该能够编写更多内容,它只是可滚动的。
为此,我使用了带有 editText 和 2 个按钮的约束布局。
下面是代码,供参考
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/customEditTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editTextPassword"
style="@style/editTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:letterSpacing="@dimen/letterSpace0_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
android:background="@android:color/transparent"
android:id="@+id/eyeIcon"
android:layout_width="@dimen/dp30"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/show_eye_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/editTextPassword"
app:layout_constraintEnd_toStartOf="@+id/fingerprintIcon"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="@dimen/dp_5"
android:elevation="@dimen/dp10"
android:padding="@dimen/dp_5"/>
<Button
android:background="@android:color/transparent"
android:id="@+id/fingerprintIcon"
android:layout_width="@dimen/dp30"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/ic_fingerprint_black_24dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="@dimen/dp_5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="@dimen/dp10"
android:padding="@dimen/dp_5"
android:elevation="@dimen/dp10"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
您的所有视图都有 app:layout_constraintTop_toTopOf="parent"
和 app:layout_constraintBottom_toBottomOf="parent"
,这意味着它们都是垂直居中的,因此它们重叠。现在你想让它们水平在同一行还是垂直在同一行?
编辑:
由于工作原因,我无法显示图片。您可以使用 Barriers 实现此目的。现在你应该阅读并理解所提供的 link,它用视觉动画详细解释了它。一开始可能看起来很难,但值得理解,没有它你将无法生存。它会让你告诉 EditText
永远不要重叠。
为什么不使用垂直方向的两个线性布局主布局和水平方向的编辑文本和按钮的线性布局?不确定这是否是您想要的...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5px">
<EditText
android:id="@+id/edit_text_password"
android:hint="Password"
android:layout_width="wrap_content"
android:inputType="textPassword"
android:layout_weight="10"
android:layout_height="wrap_content"
android:gravity="left" />
<Button
android:id="@+id/button_show_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="icon"/>
<Button
android:id="@+id/button_fingerprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="icon"/>
</LinearLayout>
</LinearLayout>
为了防止文本与 Button
重叠,您可以在 EditText
的末尾设置一个填充:
android:paddingEnd="90dp"
这样,EditText
下划线仍会显示在两个 Button
的下方。
因此,在我的密码字段中,我尝试将这些图标添加为按钮。现在我面临的问题是文本在该按钮上重叠。实际上我想要的是按钮保留在 edittext 中,但文本不会重叠,但您仍然应该能够编写更多内容,它只是可滚动的。
为此,我使用了带有 editText 和 2 个按钮的约束布局。 下面是代码,供参考
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/customEditTextLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editTextPassword"
style="@style/editTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:letterSpacing="@dimen/letterSpace0_1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
android:background="@android:color/transparent"
android:id="@+id/eyeIcon"
android:layout_width="@dimen/dp30"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/show_eye_black_24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/editTextPassword"
app:layout_constraintEnd_toStartOf="@+id/fingerprintIcon"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginEnd="@dimen/dp_5"
android:elevation="@dimen/dp10"
android:padding="@dimen/dp_5"/>
<Button
android:background="@android:color/transparent"
android:id="@+id/fingerprintIcon"
android:layout_width="@dimen/dp30"
android:layout_height="wrap_content"
android:drawableEnd="@drawable/ic_fingerprint_black_24dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="@dimen/dp_5"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="@dimen/dp10"
android:padding="@dimen/dp_5"
android:elevation="@dimen/dp10"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
您的所有视图都有 app:layout_constraintTop_toTopOf="parent"
和 app:layout_constraintBottom_toBottomOf="parent"
,这意味着它们都是垂直居中的,因此它们重叠。现在你想让它们水平在同一行还是垂直在同一行?
编辑:
由于工作原因,我无法显示图片。您可以使用 Barriers 实现此目的。现在你应该阅读并理解所提供的 link,它用视觉动画详细解释了它。一开始可能看起来很难,但值得理解,没有它你将无法生存。它会让你告诉 EditText
永远不要重叠。
为什么不使用垂直方向的两个线性布局主布局和水平方向的编辑文本和按钮的线性布局?不确定这是否是您想要的...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5px">
<EditText
android:id="@+id/edit_text_password"
android:hint="Password"
android:layout_width="wrap_content"
android:inputType="textPassword"
android:layout_weight="10"
android:layout_height="wrap_content"
android:gravity="left" />
<Button
android:id="@+id/button_show_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="icon"/>
<Button
android:id="@+id/button_fingerprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="icon"/>
</LinearLayout>
</LinearLayout>
为了防止文本与 Button
重叠,您可以在 EditText
的末尾设置一个填充:
android:paddingEnd="90dp"
这样,EditText
下划线仍会显示在两个 Button
的下方。