【问题标题】:how to change the color of outline or border of textInputLayout not focused?如何更改未聚焦的textInputLayout的轮廓或边框的颜色?
【发布时间】:2019-02-03 03:53:01
【问题描述】:

我正在尝试更改 textInputLayout 的轮廓或边框的颜色,我不知道为什么它没有改变,我搜索并找到了一些解决方案但对我不起作用。

这里我放了样式,然后应用到textInputLayout中

<style name="WhiteOutlineBox" parent="Widget.MaterialComponents.TextInputLayout.OutlineBox">
    <item name="boxStrokeColor">@color/snow </item>
    <item name="hintTextAppearance">@style/TextLabel</item>
    <item name="android:textColorHint">@color/snow</item>
    <item name="passwordToggleTint">@color/snow</item>

    <item name="colorControlNormal">@color/snow</item>
    <item name="colorControlActivated">@color/snow</item>
    <item name="colorControlHighlight">@color/snow</item>
    <item name="colorPrimary">@color/snow</item>
    <item name="colorPrimaryDark">@color/snow</item>
    <item name="colorAccent">@color/snow</item>

</style>

<!--  this style for the hint text lable in textInputLayout -->
<style name="TextLabel" parent="TextAppearance.Design.Hint">
    <item name="android:textSize">12sp</item>
    <item name="android:textColor">@color/snow</item>
</style>

这里是xml中textInputLayout的代码

<android.support.design.widget.TextInputLayout
            android:id="@+id/ed_oldPass"
            style="@style/WhiteOutlineBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="32dp"
            android:layout_marginEnd="32dp"
            android:layout_marginTop="50dp"
            app:passwordToggleEnabled="true">

            <android.support.design.widget.TextInputEditText
                style="@style/WhiteOutlineBox"
                android:layout_width="match_parent"
                android:layout_height="56dp"
                android:background="@color/snow"
                android:textColor="@color/snow"
                android:layout_marginBottom="10dp"
                android:hint="@string/old_pass_ed_hint"
                android:inputType="textPassword"
                android:paddingEnd="10dp"
                android:paddingStart="10dp" />
        </android.support.design.widget.TextInputLayout>

笔触颜色,hintTextAppearence,提示颜色和通过切换色调颜色改变但其他人没有,我想要的是改变边框的颜色,当它不集中怎么做?请帮助并提前感谢

【问题讨论】:

  • 目前我找到的最佳答案是这个 --> stackoverflow.com/a/50818399/1177959
  • @Sotti 谢谢,这很有帮助,但如果我想在某些页面中更改它,该怎么做?
  • 鉴于一切都非常hacky,我不知道该怎么做。我对 TextInputLayout 缺乏基本自定义感到非常惊讶。

标签: android colors styles android-textinputlayout


【解决方案1】:

如果只想以编程方式更改一个 TextInputLayout 则会出现问题,问题是属性 defaultStrokeColor 不可访问,更改它的唯一方法是通过覆盖颜色 mtrl_textinput_default_box_stroke_color 或使用 state list colour 但在这两种情况下,您都需要 XML 上的样式。

另一方面,focusedStrokeColor 属性可通过setBoxStrokeColor 访问,因此无需任何特殊代码即可通过编程方式更改它。

如果您可以选择反射,则解决方案当然是在运行时更改属性的可访问性,以下代码在 material-1.1.0 上完成了这项工作:

fun TextInputLayout.setDefaultStrokeColor(
    color: Int
) {
    try {
        val defaultStrokeColor = TextInputLayout::class.java.getDeclaredField("defaultStrokeColor")
        defaultStrokeColor.isAccessible = true
        defaultStrokeColor.set(this, color)
    } catch (e: NoSuchFieldException) {
        // failed to change the color
    }
}

将其用作extension function:

yourView.setDefaultStrokeColor(yourColor)

【讨论】:

    【解决方案2】:

    您可以使用 boxStrokeColor 属性。它可以与选择器一起使用。

    使用类似的东西:

        <com.google.android.material.textfield.TextInputLayout
            app:boxStrokeColor="@color/text_input_layout_stroke_color"
            ..>
    

    <style name="WhiteOutlineBox" parent="Widget.MaterialComponents.TextInputLayout.OutlineBox">
        <item name="boxStrokeColor">@color/text_input_layout_stroke_color</item>
    </style>
    

    与:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:alpha="..." android:color="@color/...." android:state_focused="true"/>
      <item android:alpha="..." android:color="@color/...." android:state_hovered="true"/>
      <item android:alpha="..." android:color="@color/...." android:state_enabled="false"/>
      <item android:alpha="..." android:color="@color/...."/>  <!-- unfocused -->
    </selector>
    



    【讨论】:

      【解决方案3】:

      将此添加到您的 color.xml

      <color name="mtrl_textinput_default_box_stroke_color">#BDC3C7</color>
      

      它会覆盖默认的未聚焦轮廓颜色

      【讨论】:

      • 但所有 textinputlayout 都发生了变化。不适用于特定的活动主题
      猜你喜欢
      • 2019-02-10
      • 2019-08-22
      • 2018-11-21
      • 2021-06-17
      • 2012-03-02
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      相关资源
      最近更新 更多