【问题标题】:Android - How to change text color in TextInputLayout (Material Components)?Android - 如何更改 TextInputLayout(材料组件)中的文本颜色?
【发布时间】:2020-07-03 02:44:56
【问题描述】:

我需要更改 TextInputLayout 文本中的颜色,而不更改原色。我只想更改用户输入的文本的颜色,而不是提示。我找不到任何属性来做到这一点......我尝试的是:

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
        <!-- Color definitions -->
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryDarkColor</item>
        <item name="colorAccent">@color/secondaryColor</item>
        <item name="colorControlHighlight">@color/secondaryLightColor</item>
        <item name="colorButtonNormal">@color/secondaryColor</item>
        <item name="colorControlActivated">@color/secondaryColor</item>
        <item name="android:windowBackground">@color/colorWhite</item>
        <item name="android:textColorPrimary">@color/primaryTextColor</item>
        <item name="android:textColorSecondary">@color/secondaryTextColor</item>

        <item name="textInputStyle">@style/inputStyle</item>
    </style>

@style/inputStyle 在哪里:

    <style name="inputStyle" parent="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
        <item name="android:textColor">@color/secondaryTextColor</item> 
    </style>

但它似乎不起作用。我注意到如果我更改 colorPrimary 文本颜色会发生变化,但正如我所说,我无法更改它。我该怎么办?

【问题讨论】:

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


    【解决方案1】:

    您需要在TextInputLayout 内放置一个TextInputEditText 小部件,并且可以照常设置textColor 属性。

    【讨论】:

    • 没用...我想我做错了什么,但我不知道是什么。我还注意到colorButtonNormal 不会改变按钮颜色,所以我认为我肯定错过了一些东西
    • 请分享您的 XML 文件。
    【解决方案2】:

    您可以通过edittext中编辑文本的android:textColor属性更改输入文本颜色:

       <com.google.android.material.textfield.TextInputLayout
         ..>
    
            <com.google.android.material.textfield.TextInputEditText
                android:textColor="#FF11AA"
                ../>
    
        </com.google.android.material.textfield.TextInputLayout>
    

    否则,由于 TextInputEditText 的文本颜色当前基于 android:textColorPrimary 颜色,您也可以使用以下方法覆盖它:

            <com.google.android.material.textfield.TextInputEditText
                android:theme="@style/customEdit"
                ../>
    

    与:

      <style name="customEdit">
        <item name="android:textColorPrimary">@color/....</item>
      </style>
    

    或者您可以为TextInputLayout 定义自定义样式:

        <com.google.android.material.textfield.TextInputLayout
            style="@style/custom"
            ..>
    

    与:

      <style name="custom" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
        <item name="materialThemeOverlay">@style/MyTextInputEditText_filledBox_custom_color</item>
      </style>
    
      <style name="MyTextInputEditText_filledBox_custom_color" parent="@style/Widget.MaterialComponents.TextInputEditText.FilledBox">
        <item name="android:textColorPrimary">@color/....</item>
      </style>
    

    【讨论】:

      【解决方案3】:

      我知道现在已经快 2 年了,但我遇到了同样的问题并寻找答案,我想出的方法是添加,

      <item name="android:textColorPrimary">@color/yourColor</item>
      

      到您的应用程序的主题.xml(样式)。这样就可以了。

      【讨论】:

        【解决方案4】:

        这是因为您将颜色应用于错误的组件。 Material 中的InputTexts 由两个不同的组件组成:TextInputLayoutTextInputEditText

        在这个Anatomy and Key Properties section of the Material Text Input Documentation你可以阅读以下注释:

        注意:下表中的所有属性都应设置在TextInputLayout上,输入文本属性除外(应设置在TextInputEditText上)。 p>

        这意味着根据您想要设置样式的TextInput 部分,您将不得不扩展不同的布局:

        TextInputLayout:Widget.MaterialComponents.TextInputLayout.*

        TextInputEditText:Widget.MaterialComponents.TextInputEditText.*

        在主题定义中,要指定应用于TextInputLayout 的样式,请使用textInputStyle,并指定TextInputEditText 的样式,请使用editTextStyle

        在您的情况下,文本颜色是TextInputEditText 样式之一,而不是TextInputLayout。所以替换:

        <item name="textInputStyle">@style/inputStyle</item>
        

        <item name="editTextStyle">@style/editTextStyle</item>
        

        <style name="inputStyle" parent="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
            <item name="android:textColor">@color/secondaryTextColor</item> 
        </style>
        

        <style name="editTextStyle" parent="@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox">
            <item name="android:textColor">@color/secondaryTextColor</item> 
        </style>
        

        这将有效地改变输入文本的文本颜色。

        更新我刚刚意识到你已经在主题中定义了colorPrimaryText

        主题的colorPrimaryTextTextInput 中文本颜色的主要颜色,所以你要么改变它,要么如果你不能因为影响其他地方,你覆盖它......但这并不容易。你必须使用materialThemeOverlay

        首先,在主题定义中同时使用textInputStyleeditTextStyle 而不是android:textColor,并在两者中指定:

        <item name="materialThemeOverlay">@style/myOverridingOverlay</item>
        

        最后,定义你的叠加层,没有父级:

        <style name="myOverridingOverlay" parent="">
            <item name="android:textColorPrimary">#123456</item>
        </style>
        

        【讨论】:

          【解决方案5】:

          只需将此代码添加到

          android:textColorHint="yourcolor"

          这会给它颜色

          【讨论】:

          • 欢迎来到 Stack Overflow!这似乎没有回答专门关于文本颜色的问题,不是提示颜色
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-09-15
          • 2020-08-15
          • 1970-01-01
          • 2018-08-06
          • 2016-05-04
          • 1970-01-01
          • 2020-06-26
          相关资源
          最近更新 更多