joh*_*son 196 android android-edittext android-design-library android-textinputlayout
参考TextInputLayout
Google发布的新版本,如何更改浮动标签文字颜色?
设置colorControlNormal
,colorControlActivated
,colorControlHighLight
在风格于事无补.
这就是我现在拥有的:
Bra*_*ani 346
尝试下面的代码它在正常状态下工作
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/TextLabel">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hiiiii"
android:id="@+id/edit_id"/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
在样式文件夹TextLabel代码中
<style name="TextLabel" parent="TextAppearance.AppCompat">
<!-- 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>
Run Code Online (Sandbox Code Playgroud)
设置为应用程序的主要主题,它只能突出显示状态
<item name="colorAccent">@color/Color Name</item>
Run Code Online (Sandbox Code Playgroud)
更新:
UnsupportedOperationException:无法在api 16或更低版本中转换为color:type = 0x2
Fan*_*ang 100
<style name="TextAppearance.App.TextInputLayout" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/red</item>
<item name="android:textSize">14sp</item>
</style>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/gray" //support 23.0.0
app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout" >
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
joh*_*son 68
找到答案,使用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>
Run Code Online (Sandbox Code Playgroud)
ces*_*rds 20
您不需要使用android:theme="@style/TextInputLayoutTheme"
更改浮动标签颜色,因为它将影响用作标签的小TextView的整个主题.相反,您可以使用以下app:hintTextAppearance="@style/TextInputLayout.HintText"
位置:
<style name="TextInputLayout.HintText">
<item name="android:textColor">?attr/colorPrimary</item>
<item name="android:textSize">@dimen/text_tiny_size</item>
...
</style>
Run Code Online (Sandbox Code Playgroud)
如果解决方案有效,请告诉我:-)
如何更改浮动标签的文字颜色?
使用Material Components库,您可以使用来自定义TextInputLayout
提示文本的颜色(需要版本1.1.0)
在布局中:
app:hintTextColor
attribute:折叠时标签的颜色,并且文本字段处于活动状态android:textColorHint
属性:所有其他文本字段状态(例如,静止和禁用)中标签的颜色<com.google.android.material.textfield.TextInputLayout
app:hintTextColor="@color/mycolor"
android:textColorHint="@color/text_input_hint_selector"
.../>
Run Code Online (Sandbox Code Playgroud)
Widget.MaterialComponents.TextInputLayout.*
:<style name="MyFilledBox" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
<item name="hintTextColor">@color/mycolor</item>
<item name="android:textColorHint">@color/text_input_hint_selector</item>
</style>
Run Code Online (Sandbox Code Playgroud)
的默认选择器android:textColorHint
是:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.38" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
<item android:alpha="0.6" android:color="?attr/colorOnSurface"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
好的,所以,我发现这个答案非常有帮助,并感谢所有做出贡献的人。不过,只是添加一些东西。接受的答案确实是正确的答案......但是......在我的情况下,我希望在EditText
小部件下方实现错误消息,app:errorEnabled="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>
Run Code Online (Sandbox Code Playgroud)
还有我的小部件:
<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>
Run Code Online (Sandbox Code Playgroud)
现在它显示黑色文本颜色而不是textColorPrimary
白色。
您可以以编程方式使用:
/* 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})
Run Code Online (Sandbox Code Playgroud)
解释:
从颜色资源中获取 int 颜色值(一种呈现 android 使用的 rgb 颜色的方法)。我写了 ColorEnabled,但对于这个答案,它真的应该是 ColorHintExpanded 和 ColorViewCollapsed。无论如何,这是当视图“TextInputLayout”处于展开或折叠状态时您将看到的颜色;您将通过在视图的函数“setDefaultHintTextColor”上使用下一个数组来设置它。参考: TextInputLayout 参考 - 在此页面中搜索方法“setDefaultHintTextColor”以获取更多信息
查看上面的文档,您可以看到这些函数使用 ColorStateList 为展开和折叠提示设置颜色。
为了创建 ColorStateList,我首先创建了一个包含我想要的状态的矩阵,在我的情况下是 state_enabled 和 state_disabled(在 TextInputLayout 中,它们等于 Hint Expanded 和 Hint Collapsed [我不记得按哪个顺序哈哈,反正我找到了它只是做一个测试])。然后我将颜色资源的 int 值数组传递给 ColorStateList 的构造函数,这些颜色与状态矩阵有对应关系(颜色数组中的每个元素都对应于状态矩阵中相同位置的相应数组)。因此,颜色数组的第一个元素将用作状态矩阵第一个数组中每个状态的颜色(在我们的例子中,该数组只有 1 个元素:启用状态 = TextInputLayut 的提示扩展状态)。最后的事情状态有正/负值,你只有正值,
希望这是有帮助的。再见有一个很好的编码(:
归档时间: |
|
查看次数: |
177911 次 |
最近记录: |