Android 如何更改TextInputLayout的浮动标签颜色
参考谷歌发布的新的Android 如何更改TextInputLayout的浮动标签颜色,android,android-edittext,android-design-library,android-textinputlayout,Android,Android Edittext,Android Design Library,Android Textinputlayout,参考谷歌发布的新的TextInputLayout,如何更改浮动标签文本颜色 在样式中设置colorControlNormal,colorControlActivated,colorControlHighLight,没有帮助 这就是我现在拥有的: 找到答案,使用android.support.design:hintTextAppearance属性设置您自己的浮动标签外观 例如: <android.support.design.widget.TextInputLayout androi
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>
@颜色/红色
14便士
您应该在此处更改颜色
<style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#673AB7</item>
<item name="colorPrimaryDark">#512DA8</item>
<item name="colorAccent">#FF4081</item>
<item name="android:windowBackground">@color/window_background</item>
</style>
#673AB7
#512DA8
#FF4081
@颜色/窗口背景
现在,只需使用colorAccent
和colorPrimary
即可完美工作。尝试以下代码,它在正常状态下工作
<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>
<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>
更新:
您是否正在使用材质组件库
<item name="colorAccent">@color/Color Name</item>
您可以在主主题中添加以下行
<item name="colorPrimary">@color/your_color</item> // Activated State
<item name="colorOnSurface">@color/your_color</item> // Normal State
@color/your\u color//激活状态
@颜色/你的颜色//正常状态
或者,您是否希望在noraml状态和激活状态下使用不同的颜色,并按照以下代码进行自定义
<style name="Widget.App.TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="materialThemeOverlay">@style/ThemeOverlay.App.TextInputLayout</item>
<item name="shapeAppearance">@style/ShapeAppearance.App.SmallComponent</item> //Changes the Shape Apperance
<!--<item name="hintTextColor">?attr/colorOnSurface</item>--> //When you added this line it will applies only one color in normal and activate state i.e colorOnSurface color
</style>
<style name="ThemeOverlay.App.TextInputLayout" parent="">
<item name="colorPrimary">@color/colorPrimaryDark</item> //Activated color
<item name="colorOnSurface">@color/colorPrimary</item> //Normal color
<item name="colorError">@color/colorPrimary</item> //Error color
//Text Appearance styles
<item name="textAppearanceSubtitle1">@style/TextAppearance.App.Subtitle1</item>
<item name="textAppearanceCaption">@style/TextAppearance.App.Caption</item>
<!--Note: When setting a materialThemeOverlay on a custom TextInputLayout style, don’t forget to set editTextStyle to either a @style/Widget.MaterialComponents.TextInputEditText.* style or to a custom one that inherits from that.
The TextInputLayout styles set materialThemeOverlay that overrides editTextStyle with the specific TextInputEditText style needed. Therefore, you don’t need to specify a style tag on the edit text.-->
<item name="editTextStyle">@style/Widget.MaterialComponents.TextInputEditText.OutlinedBox</item>
</style>
<style name="TextAppearance.App.Subtitle1" parent="TextAppearance.MaterialComponents.Subtitle1">
<item name="fontFamily">@font/your_font</item>
<item name="android:fontFamily">@font/your_font</item>
</style>
<style name="TextAppearance.App.Caption" parent="TextAppearance.MaterialComponents.Caption">
<item name="fontFamily">@font/your_font</item>
<item name="android:fontFamily">@font/your_font</item>
</style>
<style name="ShapeAppearance.App.SmallComponent" parent="ShapeAppearance.MaterialComponents.SmallComponent">
<item name="cornerFamily">cut</item>
<item name="cornerSize">4dp</item>
</style>
@style/ThemeOverlay.App.TextInputLayout
@style/ShapeAppearance.App.SmallComponent//更改形状外观
//当您添加这一行时,它将在正常和激活状态下仅应用一种颜色,即colorOnSurface color
@颜色/colorPrimaryDark//激活颜色
@颜色/原色//正常颜色
@颜色/colorPrimary//错误颜色
//文本外观样式
@style/TextAppearance.App.Subtitle1
@style/TextAppearance.App.Caption
@style/Widget.MaterialComponents.textInputItemText.OutlineBox
@字体/你的
@字体/你的
@字体/你的
@字体/你的
切
4dp
将下面的行添加到主主题中,或者可以在xml中将样式设置为textinputlayout
<item name="textInputStyle">@style/Widget.App.TextInputLayout</item>
@style/Widget.App.TextInputLayout
好的,我觉得这个答案非常有帮助,感谢所有的贡献者。不过,我只是想补充一点。被接受的答案确实是正确的答案……但是……在我的例子中,我想用app:errorEnabled=“true”
实现EditText
小部件下面的错误消息,这一行让我的生活很艰难。这似乎覆盖了我为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>
@color/my\u yellow
@颜色/我的\黄色\深色
@颜色/我的\黄色\深色
@android:彩色/白色
@颜色/深灰色
@颜色/浅灰色
真的
假的
@颜色/深灰色
@android:彩色/黑色
@android:彩色/白色
和我的小部件:
<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
白色。我建议您为TextInputLayout创建样式主题,只更改重音颜色。将父项设置为应用程序基础主题:
<style name="MyTextInputLayout" parent="MyAppThemeBase">
<item name="colorAccent">@color/colorPrimary</item>
</style>
<android.support.design.widget.TextInputLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:theme="@style/MyTextInputLayout">
@颜色/原色
在最新版本的支持库(23.0.0+)中,TextInputLayout
采用XML中的以下属性来编辑浮动标签颜色:android:textColorHint=“@color/white”
我解决了这个问题。
这是布局图:
<android.support.design.widget.TextInputLayout
android:id="@+id/til_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username"
>
<android.support.v7.widget.AppCompatEditText android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
/>
</android.support.design.widget.TextInputLayout>
这是一种风格:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
<item name="colorAccent">@color/pink</item>
<item name="colorControlNormal">@color/purple</item>
<item name="colorControlActivated">@color/yellow</item>
</style>
@颜色/粉红色
@颜色/紫色
@颜色/黄色
您应该在应用程序中使用您的主题:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
</application>
在关注文本标签时更改其颜色。i、 e.打字。您必须添加指定
<item name="android:textColorPrimary">@color/yourcolorhere</item>
@color/yourcolorhere
请注意:
您必须将所有这些实现添加到主主题中 它对我有用。。。。。
在TextInputLayout中添加提示颜色
<android.support.design.widget.TextInputLayout
android:textColorHint="#ffffff"
android:id="@+id/input_layout_password"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edtTextPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:hint="Password"
android:inputType="textPassword"
android:singleLine="true"
/>
</android.support.design.widget.TextInputLayout>
您不需要使用android:theme=“@style/TextInputLayoutTheme”
来更改浮动标签颜色,因为它将影响用作标签的小文本视图的整个主题。相反,您可以使用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>
?属性/颜色初级
@尺寸/文字尺寸
...
让我知道解决方案是否有效:-)要更改提示的颜色并编辑文本下划线颜色:colorControlActivated 更改字符计数器颜色:textColorSecondary 更改错误消息颜色:colorControlNormal 更改密码可见性按钮色调:彩色前景 有关TextInputLayout的更多信息,请阅读
@颜色/原色
@颜色/原色暗
#e91e63
#33691e
#f57f17
#673ab7
我尝试在android.support.design.widget.TextInputLayout中使用android:textColorHint,效果很好
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="@color/colorAccent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Hello"
android:imeActionLabel="Hello"
android:imeOptions="actionUnspecified"
android:maxLines="1"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
#fff
#fff
将其添加到样式中,并将TextInputLayout Theam设置为App2,它将正常工作;) 我宁愿使用Widget.Design.TextInputLayout作为父级,而不是Brahmam Yamani答案。这样可以确保所有必需项都存在,即使不是所有项都被覆盖。在Yamanis answer中,如果调用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>
@颜色/颜色名称
20便士
@颜色/颜色名称
@颜色/颜色名称
@颜色/颜色名称
在我的例子中,我在我的TextInputLayout小部件中添加了这个“应用程序:hintTextAppearance=“@color/colorPrimaryDark”
。
<com.google.android.material.textfield.TextInputLayout
android:hint="Hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/TextInputLayoutHint">
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:paddingTop="@dimen/_5sdp"
android:paddingBottom="@dimen/_5sdp"
android:textColor="#000000"
android:textColorHint="#959aa6" />
</com.google.android.material.textfield.TextInputLayout>
res/values/styles.xml
<style name="TextInputLayoutHint" parent="">
<item name="android:textColorHint">#545454</item>
<item name="colorControlActivated">#2dbc99</item>
<item name="android:textSize">11sp</item>
</style>
#545454
#2dbc99
11便士
如何更改浮动标签文本颜色?
<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>
<com.google.android.material.textfield.TextInputLayout
android:hint="Hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/TextInputLayoutHint">
<androidx.appcompat.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:maxLines="1"
android:paddingTop="@dimen/_5sdp"
android:paddingBottom="@dimen/_5sdp"
android:textColor="#000000"
android:textColorHint="#959aa6" />
</com.google.android.material.textfield.TextInputLayout>
<style name="TextInputLayoutHint" parent="">
<item name="android:textColorHint">#545454</item>
<item name="colorControlActivated">#2dbc99</item>
<item name="android:textSize">11sp</item>
</style>
<com.google.android.material.textfield.TextInputLayout
app:hintTextColor="@color/mycolor"
android:textColorHint="@color/text_input_hint_selector"
.../>
<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>
<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>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/app_name"
app:hintTextColor="@android:color/white">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</com.google.android.material.textfield.TextInputLayout>
/* 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})
<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>
<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>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorControlActivated">@color/yellow_bright</item>
<item name="colorControlNormal">@color/yellow_black</item>
</style>
app:textColorHint="@color/textcolor_black"
android:textColorHint="@color/textcolor_black"
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/black_txt" android:state_enabled="true" />
<item android:color="@color/black_txt" android:state_selected="true" />
<item android:color="@color/txtColorGray" android:state_selected="false" />
<item android:color="@color/txtColorGray" android:state_enabled="false" />
</selector>