【问题标题】:ChangeTextInputLayout Title Color更改 TextInputLayout 标题颜色
【发布时间】:2019-04-27 08:07:22
【问题描述】:

我已将我的项目迁移到 androidX 并使用带有材料设计组件的新材料主题。我能够为我的 TextInputLayout 使用新的 Outlined 样式,目前它以白色显示标题。我尝试将样式设置为TextInputLayout

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

但它以白色显示标题。

之后我在styles.xml创建了单独的样式

<style name="TextLabel" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
    <item name="boxStrokeColor">@color/colorAccent</item>
    <item name="android:textColorHint">@color/colorAccent</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:textColorHint">@color/colorAccent</item>
    <item name="android:textColor">@color/white</item>
    <item name="colorControlNormal">@color/colorAccent</item>
    <item name="colorControlActivated">@color/colorAccent</item>
    <item name="colorControlHighlight">@color/colorAccent</item>
</style>

我在我的 xml 中使用这种样式,如下所示。

<com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/pad_small"
        android:hint="Search Store"
        style="@style/TextLabel">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/et_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/ic_search"/>
</com.google.android.material.textfield.TextInputLayout>

我正在使用的 gradle 导入是

implementation 'com.google.android.material:material:1.1.0-alpha01'

布局如下所示,我错过了什么?如何更改TextInputLayout 的标题颜色?目前我已经设置了深色背景,所以文本是可见的,但我想使用白色背景,当我这样做时,标题会与背景混合并且不可见。

【问题讨论】:

    标签: android android-layout android-textinputlayout androidx mdc-components


    【解决方案1】:

    尝试使用提示颜色?在布局?在正常情况下有效的方法不适用于材料设计 我的意思是hintColor这种方式

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/pad_small"
        android:hintColor="#0000"
        android:hint="Search Store"
        style="@style/TextLabel">
    

    【讨论】:

    • 不在样式文件中,而是在 xml 文件中。
    • 我更新了答案,你的设计是否依赖于 androidX 材料设计?
    【解决方案2】:

    尝试使用

    android:textColorHighlight="@color/when_focused"
    android:textColorHint="@color/not_focused"
    

    像这样改变你的styles.xml

    <style name="TextLabel"parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="boxStrokeColor">@color/colorAccent</item>
        <item name="android:textColorHint">@color/colorAccent</item>
        <item name="android:textColor">@color/white</item>
        <item name="android:textColorHighlight="@color/when_focused"</item>
     </style>
    

    如果这不起作用,请尝试在 XML 中直接使用这两个

    <com.google.android.material.textfield.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/pad_small"
        android:textColorHighlight="@color/when_focused"
        android:textColorHint="@color/not_focused"
        android:hint="Search Store"
        style="@style/TextLabel">
    
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/et_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableRight="@drawable/ic_search"/>
     </com.google.android.material.textfield.TextInputLayout>
    

    【讨论】:

      猜你喜欢
      • 2018-11-21
      • 2020-12-16
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 2020-08-15
      • 2020-06-05
      • 2021-08-25
      相关资源
      最近更新 更多