【问题标题】:TextInputLayout move the hint label and change it's background color when focusedTextInputLayout 移动提示标签并在聚焦时更改其背景颜色
【发布时间】:2020-03-20 00:37:33
【问题描述】:

我正在尝试自定义材料TextInpuLayout.OutlinedBoxTextInputEditText

我现在的状态如下图

我想要做的是删除提示标签的背景,这样它就不会产生丑陋的切口。像这样:

或者,如果不可能将标签移到输入上方也可以解决问题:

如果可以使用样式来实现,那就太好了,这样我也可以轻松地将其应用于其他文本输入元素。 我对android还很陌生,所以请体谅。

这是样式的代码:

<style name="MyTheme" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="android:textSize">14sp</item>
    <item name="boxCornerRadiusTopStart">@dimen/textInputCornerRadius</item>
    <item name="boxCornerRadiusTopEnd">@dimen/textInputCornerRadius</item>
    <item name="boxCornerRadiusBottomStart">@dimen/textInputCornerRadius</item>
    <item name="boxCornerRadiusBottomEnd">@dimen/textInputCornerRadius</item>
    <item name="boxBackgroundColor">@color/primaryDarkColorBackground</item>
    <item name="boxStrokeColor">@color/text_input_box_stroke</item>
</style>
<style name="EditTextStyle" parent="Widget.AppCompat.EditText">
    <item name="android:background">@null</item>
    <item name="android:paddingStart">30dp</item>
</style>

这是我在布局中定义输入的方式:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/username_layout"
        style="@style/MyTheme"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="48dp"
        android:layout_marginEnd="48dp"
        android:hint="@string/email_or_username"
        app:boxBackgroundMode="outline"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.10">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/username"
            style="@style/EditTextStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </com.google.android.material.textfield.TextInputLayout>

【问题讨论】:

  • 你能提供一些你目前已经实现的代码吗?
  • @Swayangjit 它只是默认的轮廓框,带有透明框描边、背景颜色和一点填充。
  • 分享你的xml代码。
  • 添加了一些xml代码进行说明。
  • 我遇到了同样的问题。我正在构建一个 TextInputLayout 组件,我不知道它将被放置在哪个背景上,并且 HintText 背景与上面/OP 中的第一个示例具有相同的问题(文本周围的正方形)。也没有找到以编程方式定位它的方法。

标签: android android-textinputlayout material-components material-components-android android-textinputedittext


【解决方案1】:

我认为您无法通过 TextInputLayout.OutlinedBox 样式获得它。
这不是您正在寻找的:

   <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Rounded"
        ..>

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingStart="30dp"
            />

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

与:

  <style name="Widget.MaterialComponents.TextInputLayout.FilledBox.Rounded" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    <item name="shapeAppearanceOverlay">@style/Rounded_ShapeAppearanceOverlay.MaterialComponents.TextInputLayout</item>
    <item name="boxStrokeColor">@color/input_text_no_underline</item>
  </style>

  <style name="Rounded_ShapeAppearanceOverlay.MaterialComponents.TextInputLayout" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
  </style>

选择器用于去除下划线:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:alpha="0" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
  <item android:alpha="0" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
  <item android:alpha="0" android:color="?attr/colorOnSurface"/>
</selector>

注意:需要1.1.0版本的库。

【讨论】:

  • 嗨,Gabriele,这个答案对我来说非常有用,因为我面临着同样的问题。我遵循了您的解决方案,但下划线仍然存在。我已将选择器放在 color.xml 文件中,如下所示: schemas.android.com/apk/res/android"> ..... 也许问题是 ?attr/colorOnSurface?我没有 attr 目录。再次感谢您的帮助
  • @michelecasati 尝试使用与背景相同的颜色
  • 我已经理解了这个问题。我使用渐变作为背景,所以在这两种情况下颜色都不匹配。我必须想出另一个解决方案,无论如何感谢您的快速帮助
【解决方案2】:

尝试在TextInputEditText上设置:

android:background="@android:color/transparent"

【讨论】:

    猜你喜欢
    • 2016-11-14
    • 2017-10-16
    • 2018-01-06
    • 2020-06-26
    • 2020-06-05
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多