参考谷歌发布的新TextInputLayout,如何更改浮动标签文本颜色?

在样式中设置colorControlNormal, colorControlActivated, colorControlHighLight没有帮助。

这是我现在拥有的:


当前回答

找到答案,使用android.support.design:hintTextAppearance属性设置自己的浮动标签外观。

例子:

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:hintTextAppearance="@style/TextAppearance.AppCompat">

    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/prompt_password"/>
</android.support.design.widget.TextInputLayout>

其他回答

有太多复杂的解决方案。下面是一个改变浮动标签颜色的程序。

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

除了更改其他属性:

更改框描边颜色:

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

更改方框描边宽度:

<com.google.android.material.textfield.TextInputLayout 
     app:boxStrokeWidth="1.5dp"

更改编辑TextInputEditText提示颜色:

<com.google.android.material.textfield.TextInputEditText
     android:textColorHint="@color/white"

更改TextInputEditText的颜色:

<com.google.android.material.textfield.TextInputEditText           
            android:textColor="@color/white" />

你可以在程序上使用:

/* Here you get int representation of an HTML color resources */
int yourColorWhenEnabled = ContextCompat.getColor(getContext(), R.color.your_color_enabled);
int yourColorWhenDisabled = ContextCompat.getColor(getContext(), R.color.your_color_disabled);

/* Here you get matrix of states, I suppose it is a matrix because using a matrix you can set the same color (you have an array of colors) for different states in the same array */
int[][] states = new int[][]{new int[]{android.R.attr.state_enabled}, new int[]{-android.R.attr.state_enabled}};

/* You pass a ColorStateList instance to "setDefaultHintTextColor" method, remember that you have a matrix for the states of the view and an array for the colors. So the color in position "colors[0x0]" will be used for every states inside the array in the same position inside the matrix "states", so in the array "states[0x0]". So you have "colors[pos] -> states[pos]", or "colors[pos] -> color used for every states inside the array of view states -> states[pos] */
myTextInputLayout.setDefaultHintTextColor(new ColorStateList(states, new int[]{yourColorWhenEnabled, yourColorWhenDisabled})

解释:

Get int color value from a color resource (a way to present rgb colors used by android). I wrote ColorEnabled, but really it should be, for this answer, ColorHintExpanded & ColorViewCollapsed. Anyway this is the color you will see when the hint of a view "TextInputLayout" is on Expanded or Collapsed state; you will set it by using next array on function "setDefaultHintTextColor" of the view. Reference: Reference for TextInputLayout - search in this page the method "setDefaultHintTextColor" for more info

查看上面的文档,你可以看到函数通过使用ColorStateList来设置展开和折叠提示的颜色。

ColorStateList docs

To create the ColorStateList I first created a matrix with the states I want, in my case state_enabled & state_disabled (whose are, in TextInputLayout, equals to Hint Expanded and Hint Collapsed [I don't remember in which order lol, anyway I found it just doing a test]). Then I pass to the constructor of the ColorStateList the arrays with int values of color resources, these colors have a correspondences with the states matrix (every element in colors array correspond to the respective array in states matrix at same position). So the first element of the colors array will be used as color for every state in the first array of the states matrix (in our case the array has only 1 element: enabled state = hint expanded state for TextInputLayut). Last things states have positive / negative values, and you have only the positive values, so the state "disabled" in android attrs is "-android.state.enabled", the state "not focused" is "-android.state.focused" ecc.. ecc..

希望这对你有帮助。 再见,祝你编码愉快。

而不是Brahmam Yamani的答案,我更喜欢使用Widget.Design.TextInputLayout作为父。这确保了所有必需的项都存在,即使不是所有项都被覆盖。在Yamanis的答案中,如果调用setErrorEnabled(true),应用程序将崩溃与一个无法解析的资源。

简单地更改样式如下:

<style name="TextLabel" parent="Widget.Design.TextInputLayout">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textColorHint">@color/Color Name</item> 
    <item name="android:textSize">20sp</item>
    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">@color/Color Name</item>
    <item name="colorControlNormal">@color/Color Name</item>
    <item name="colorControlActivated">@color/Color Name</item>
 </style>

从文档中可以看到:

提示应该设置在TextInputLayout上,而不是EditText上。如果在XML中的子EditText上指定了提示,TextInputLayout可能仍然正确工作;TextInputLayout将使用EditText的提示作为浮动标签。但是,将来修改提示的调用将不会更新TextInputLayout的提示。为了避免意外的行为,在TextInputLayout上调用setHint(CharSequence)和getHint(),而不是在EditText上。

所以我设置android:hint和app:hintTextColor在TextInputLayout,而不是在TextInputEditText和它工作。

好的,所以,我发现这个答案非常有用,感谢所有贡献的人。只是补充一点。公认的答案确实是正确的答案…但是…在我的情况下,我正在寻找实现下面的错误消息EditText小部件与app: errorrenabled ="true"和这一行使我的生活困难。似乎这覆盖了我为android.support.design.widget. textinputlayout选择的主题,它有一个由android:textColorPrimary定义的不同的文本颜色。

最后,我将文本颜色直接应用到EditText小部件上。我的代码是这样的:

styles.xml

<item name="colorPrimary">@color/my_yellow</item>
<item name="colorPrimaryDark">@color/my_yellow_dark</item>
<item name="colorAccent">@color/my_yellow_dark</item>
<item name="android:textColorPrimary">@android:color/white</item>
<item name="android:textColorSecondary">@color/dark_gray</item>
<item name="android:windowBackground">@color/light_gray</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="android:textColorHint">@color/dark_gray</item>
<item name="android:colorControlNormal">@android:color/black</item>
<item name="android:colorControlActivated">@android:color/white</item>

我的小部件:

<android.support.design.widget.TextInputLayout
        android:id="@+id/log_in_layout_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:errorEnabled="true">

        <EditText
            android:id="@+id/log_in_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:textColor="@android:color/black"
            android:ems="10"
            android:hint="@string/log_in_name"
            android:inputType="textPersonName" />
</android.support.design.widget.TextInputLayout>

现在它显示黑色文本颜色而不是textColorPrimary白色。