【问题标题】:How to not overlap the text on buttons in EditText?如何不重叠 EditText 中按钮上的文本?
【发布时间】:2019-11-26 16:28:13
【问题描述】:

因此,在我的密码字段中,我尝试将这些图标添加为按钮。现在我面临的问题是该按钮上的文本重叠。实际上我想要的是按钮保留在编辑文本中,但文本不会在其上重叠,但您仍然应该能够编写更多内容,它只是可滚动的。

为此,我使用了带有 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>

【问题讨论】:

    标签: android android-edittext android-button


    【解决方案1】:

    为了防止文本与Buttons 重叠,您可以在EditText 的末尾设置一个填充:

    android:paddingEnd="90dp"
    

    这样,EditText 下划线仍将显示在两个 Buttons 下方。

    【讨论】:

    • 感谢您提供快速方便的解决方案。它也适用于所有屏幕尺寸,如平板电脑、手机
    【解决方案2】:

    为什么不使用垂直方向的两个线性布局主布局和水平方向的编辑文本和按钮的线性布局?不知道这是不是你想要的...

    <?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>
    

    【讨论】:

      【解决方案3】:

      您所有的视图都有app:layout_constraintTop_toTopOf="parent"app:layout_constraintBottom_toBottomOf="parent",这意味着它们都垂直居中,因此它们重叠。现在你想让它们水平在同一行还是垂直在同一行?

      编辑: 在工作中,我无法显示图片。您可以使用Barriers 实现此目的。现在您应该阅读并理解提供的链接,该链接通过视觉动画详细解释了它。一开始可能看起来很难,但值得理解,没有它你将无法生存。它将允许您告诉EditText 永远不要重叠。

      【讨论】:

      • 您好布局很好,我的问题是当您在密码字段中输入文本时,它会与这些按钮重叠并继续
      • 您的密码视图的宽度为match_parent,同时为app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"。这正常吗?水平方向也是一样的。现在您可以使用约束布局的Guidelines 和事件Barriers 开始和结束来确保它不重叠。
      • 好吧,我明白了你想说的,我从那一点改变了我的 editText 约束,也许 idk 看起来不错。你可以给我一些建议app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toStartOf="@id/eyeIcon" app:layout_constraintTop_toTopOf="parent"
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多