【问题标题】:How to show border on TextInputLayout in Android如何在 Android 中的 TextInputLayout 上显示边框
【发布时间】:2021-04-06 04:27:14
【问题描述】:

在我的应用程序中,我想使用 TextInputLayout 并为此视图设置边框。
比如这张图片:Image

我写以下代码:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/loginFrag_phoneInputLayout"
    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/_15mdp"
    android:layout_marginLeft="@dimen/_15mdp"
    android:layout_marginTop="@dimen/_15mdp"
    android:layout_marginEnd="@dimen/_15mdp"
    android:layout_marginRight="@dimen/_15mdp"
    android:hint="@string/insertYourPhoneNumber"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/loginFrag_title">

    <com.google.android.material.textfield.TextInputEditText
        android:id="@+id/signInFrag_phoneEdt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:gravity="right|center_vertical"
        android:inputType="phone"
        android:maxLength="11"
        android:maxLines="1"
        android:paddingLeft="@dimen/_10mdp"
        android:paddingRight="@dimen/_10mdp"
        android:singleLine="true"
        android:textColorHint="@color/colorLightGray"
        android:textSize="@dimen/_10mdp"
        app:drawableRightCompat="@drawable/ic_phone" />

</com.google.android.material.textfield.TextInputLayout>

我添加了这一行:

style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"

但是当将材料设计库更新到 v1.3 时不会显示任何边框!

'com.google.android.material:material:1.3.0'

但在 v1.2v1.1 中显示边框并没有任何问题!

为什么不在 v1.3 中显示边框?
我该如何解决?

【问题讨论】:

  • 如何将它包裹在一个有背景的视图中并给它一些边距?例如,
  • 基本上你希望一个电话图标应该显示在编辑框中,这就是为什么你使用 app:drawableRightCompat="@drawable/ic_phone" 以及透明背景来使图标可见,我正确吗?
  • @NRUSINGHAMOHARANA,是的,但我该如何解决?
  • 在 TextInputLayout 中使用 app:startIconDrawable="@drawable/ic_action_phone" 并从 TextInputEditText 中删除 android:background="@android:color/transparent" 和 app:drawableRightCompat="@drawable/ic_phone"。我更新了我的答案。
  • 在theme.xml中你还需要设置材质主题prntscr.com/115fzwe

标签: android xml android-layout material-design android-textinputlayout


【解决方案1】:

您必须移除android:background="@android:color/transparent" 中的背景TextInputEditText

也不要在TextInputEditText 中使用app:drawableRightCompat="@drawable/....",而是使用:

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        app:endIconDrawable="@drawable/..."
        app:endIconMode="custom"

【讨论】:

  • 感谢我的朋友,我解决了我的问题。但是我的语言是 RTL 当点击 EditText 时显示提示就行了!!请看这张图片:i.ibb.co/XxDqNz4/39.png 我该如何解决?谢谢
  • @Miss.Ellen 发生这种情况是因为您将编辑文本 android:gravity 设置为右侧,因为它处于 RTL 中,所以它会反转,因此您不需要指定任何重力。
  • yeeeeees。感谢我的朋友的帮助。没关系
【解决方案2】:

我也在使用 v 1.3.0 并使用以下代码获得正确的边框:

 <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/loginFrag_phoneInputLayout"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginRight="10dp"
        android:hint="Phone Number"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:startIconDrawable="@drawable/ic_action_phone"
        >

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/signInFrag_phoneEdt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:maxLength="11"
            android:maxLines="1"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:singleLine="true"/>

    </com.google.android.material.textfield.TextInputLayout>

【讨论】:

  • 感谢我的朋友,但再次不显示任何边框
  • 你现在可以检查我的答案。我正在使用 v 1.3.0 并获得正确的边框。
  • 是的,我的朋友,在您的代码中显示边框。但不在我的代码中显示边框?我的代码和你的代码没有任何不同!为什么?
  • 您可以通过删除 app:boxStrokeColor="@color/teal_200" 来尝试使用您的代码吗?我希望这条线有所不同。
  • 请看我的帖子,我用你的代码更新了我的代码。请看上面的帖子。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-26
  • 2020-05-21
  • 2020-04-03
  • 2011-08-16
  • 2015-06-28
  • 2019-02-10
  • 2017-09-25
相关资源
最近更新 更多