【问题标题】:Change TextInputEditText Hint Color更改 TextInputEditText 提示颜色
【发布时间】:2017-12-17 04:43:25
【问题描述】:

我想更改我的 TextInputEditText 提示的文本颜色。似乎无论我改变什么,颜色始终是主要应用程序主题的颜色。

            <android.support.design.widget.TextInputLayout
                android:id="@+id/enter_template_name_edit_text_input_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColorHint="@color/warm_grey">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/template_name_text_input_edit_text"
                    style="@style/EditTextBaseStyle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="start"
                    android:hint="@string/add_nitrogen_template_name_label"
                    android:imeOptions="actionNext"
                    android:inputType="textCapWords|textNoSuggestions"
                    android:maxLines="1"
                    android:paddingBottom="@dimen/md_content_padding_bottom"
                    android:textAlignment="textStart"
                    android:textColorHint="@color/warm_grey"
                    android:textSize="32sp" />

以及基本样式

<style name="EditTextBaseStyle" parent="Widget.AppCompat.EditText">
    <item name="android:textViewStyle">@style/TextViewStyle</item>
    <item name="android:textColor">@color/middle_black</item>
    <item name="android:textStyle">normal</item>
    <item name="android:textSize">16sp</item>
</style>

我已经尝试添加

android:textColorHint="@color/warm_grey"

对于基本样式以及我的 AppTheme 并没有帮助。

这是我的应用主题

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/primary</item>
    <item name="android:textColorHint">@color/primary_text</item>
    <item name="android:windowBackground">@null</item>
    <item name="android:windowSoftInputMode">stateHidden|adjustPan</item>
</style>

我在这里遗漏了什么?

谢谢, 水獭

编辑:当字段没有有焦点时,提示是正确的颜色。当字段获得焦点时,提示会在字段上方移动并显示不正确的颜色。

【问题讨论】:

标签: android android-layout android-textinputlayout


【解决方案1】:

您是否也为您的AppTheme 尝试过这些属性?

<item name="colorControlNormal">@color/primary</item>
<item name="colorControlHighlight">@color/warm_grey</item>      
<item name="colorControlActivated">@color/warm_grey</item>

或移动

android:textColorHint="@color/warm_grey"

从 XML 小部件到 EditTextBaseStyle

【讨论】:

    【解决方案2】:

    试试这个:

     <style name="AppTheme.EditTextBaseStyle" parent="@android:style/TextAppearance">
        <item name="android:textColor">@android:color/darker_gray</item>
    </style>
    

    【讨论】:

      【解决方案3】:

      我能够通过添加成功地让它工作

      app:hintTextAppearance="@style/EditTextHintStyle"
      

      致我的TextInputLayout

      这是正确的样式

      <style name="EditTextHintStyle" parent="EditTextBaseStyle">
          <item name="android:textColor">@color/warm_grey</item>
      </style>
      

      【讨论】:

        【解决方案4】:

        对我来说,解决方法是在 TextInputLayout 上设置提示和 textColor,而不是在 TextInputEditText 上设置,如下所述: https://material.io/develop/android/components/text-input-layout/

            <android.support.design.widget.TextInputLayout
                android:id="@+id/passwordTextInputLayout"
                android:hint="@string/add_nitrogen_template_name_label"
                android:textColorHint="@color/warm_grey">
        
                <android.support.design.widget.TextInputEditText
                    android:textAppearance="@style/TextAppearance.Regular"
                    android:textColor="@color/white"/>
            </android.support.design.widget.TextInputLayout>
        

        【讨论】:

          【解决方案5】:

          您可以在布局中使用 app:hintTextColorandroid:textColorHint 属性:

          <com.google.android.material.textfield.TextInputLayout
              app:hintTextColor="@color/mycolor"
              android:textColorHint="@color/text_input_hint_selector"
              .../>
          

          或者您可以以自定义样式定义它们:

          <style name="..." parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
              <!-- The color of the label when it is collapsed and the text field is active -->
              <item name="hintTextColor">@color/my_color</item>
          
              <!-- The color of the label in all other text field states (such as resting and disabled) -->
              <item name="android:textColorHint">@color/my_selector_color</item>
          </style>
          

          【讨论】:

            猜你喜欢
            • 2020-01-25
            • 2021-05-31
            • 2020-02-15
            • 1970-01-01
            • 1970-01-01
            • 2022-01-07
            • 2021-08-25
            • 1970-01-01
            • 2018-02-26
            相关资源
            最近更新 更多