我正在使用Android设计库TextinputLayout
。但无法在EditText
内自定义提示颜色,标签颜色和TextinputLayout
的下划线颜色。请帮忙。
答案 0 :(得分:224)
<item name="colorControlNormal">#c5c5c5</item>
<item name="colorControlActivated">@color/accent</item>
<item name="colorControlHighlight">@color/accent</item>
有关详细信息,请选中THIS THREAD。
<style name="TextAppearance.App.TextInputLayout" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/main_color</item>
并使用它:
<android.support.design.widget.TextInputLayout
...
app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout" >
<强>更新强>
如果你想改变Hint Color,当它不是浮动标签时,就像下面一样使用它:
<android.support.design.widget.TextInputLayout
...
app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout"
android:textColorHint="#c1c2c4">
感谢@AlbAtNf
答案 1 :(得分:28)
基于Fedor Kazakov和其他答案,我创建了一个默认配置。
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="Widget.Design.TextInputLayout" parent="AppTheme">
<item name="hintTextAppearance">@style/AppTheme.TextFloatLabelAppearance</item>
<item name="errorTextAppearance">@style/AppTheme.TextErrorAppearance</item>
<item name="counterTextAppearance">@style/TextAppearance.Design.Counter</item>
<item name="counterOverflowTextAppearance">@style/TextAppearance.Design.Counter.Overflow</item>
</style>
<style name="AppTheme.TextFloatLabelAppearance" parent="TextAppearance.Design.Hint">
<!-- Floating label appearance here -->
<item name="android:textColor">@color/colorAccent</item>
<item name="android:textSize">20sp</item>
</style>
<style name="AppTheme.TextErrorAppearance" parent="TextAppearance.Design.Error">
<!-- Error message appearance here -->
<item name="android:textColor">#ff0000</item>
<item name="android:textSize">20sp</item>
</style>
</resources>
activity_layout.xml
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Text hint here"
android:text="5,2" />
</android.support.design.widget.TextInputLayout>
聚焦:
没有焦点:
错误讯息:
答案 2 :(得分:13)
此Blog Post描述了由EditText
包裹的AutoCompleteTextView
和TextInputLayout
的各种样式方面。
对于EditText
和 AppCompat lib 22.1.0 + ,您可以使用一些与主题相关的设置来设置主题属性:
<style name="StyledTilEditTextTheme">
<item name="android:imeOptions">actionNext</item>
<item name="android:singleLine">true</item>
<item name="colorControlNormal">@color/greyLight</item>
<item name="colorControlActivated">@color/blue</item>
<item name="android:textColorPrimary">@color/blue</item>
<item name="android:textSize">@dimen/styledtil_edit_text_size</item>
</style>
<style name="StyledTilEditText">
<item name="android:theme">@style/StyledTilEditTextTheme</item>
<item name="android:paddingTop">4dp</item>
</style>
并将其应用于EditText
:
<EditText
android:id="@+id/etEditText"
style="@style/StyledTilEditText"
对于AutoCompleteTextView
事情更复杂,因为将其包装在TextInputLayout
中并应用此主题会破坏浮动标签行为。
您需要在代码中修复此问题:
private void setStyleForTextForAutoComplete(int color) {
Drawable wrappedDrawable = DrawableCompat.wrap(autoCompleteTextView.getBackground());
DrawableCompat.setTint(wrappedDrawable, color);
autoCompleteTextView.setBackgroundDrawable(wrappedDrawable);
}
和Activity.onCreate
:
setStyleForTextForAutoComplete(getResources().getColor(R.color.greyLight));
autoCompleteTextView.setOnFocusChangeListener((v, hasFocus) -> {
if(hasFocus) {
setStyleForTextForAutoComplete(getResources().getColor(R.color.blue));
} else {
if(autoCompleteTextView.getText().length() == 0) {
setStyleForTextForAutoComplete(getResources().getColor(R.color.greyLight));
}
}
});
答案 3 :(得分:9)
在Edittext标签中添加此属性并享受:
android:backgroundTint="@color/colorWhite"
答案 4 :(得分:7)
通过Material Components Library,您可以使用com.google.android.material.textfield.TextInputLayout
。
您可以应用自定义样式来更改颜色。
要更改提示颜色,您必须使用以下属性:
hintTextColor
和 android:textColorHint
。
<style name="Custom_textinputlayout_filledbox" parent="@style/Widget.MaterialComponents.TextInputLayout.FilledBox">
<!-- The color of the label when it is collapsed and the text field is active -->
<item name="hintTextColor">?attr/colorPrimary</item>
<!-- The color of the label in all other text field states (such as resting and disabled) -->
<item name="android:textColorHint">@color/selector_hint_text_color</item>
</style>
您应该为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>
要更改底线颜色,您必须使用以下属性: boxStrokeColor
。
<style name="Custom_textinputlayout_filledbox" parent="@style/Widget.MaterialComponents.TextInputLayout.FilledBox">
....
<item name="boxStrokeColor">@color/selector_stroke_color</item>
</style>
在这种情况下,您还应该使用选择器。像这样:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="?attr/colorPrimary" android:state_focused="true"/>
<item android:alpha="0.87" android:color="?attr/colorOnSurface" android:state_hovered="true"/>
<item android:alpha="0.12" android:color="?attr/colorOnSurface" android:state_enabled="false"/>
<item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
</selector>
您还可以在布局中应用以下属性:
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
app:boxStrokeColor="@color/selector_stroke_color"
app:hintTextColor="?attr/colorPrimary"
android:textColorHint="@color/selector_hint_text_color"
...>
答案 5 :(得分:6)
如果您想更改TextInputLayout
的条形/线条颜色和提示文字颜色(通常是重点颜色),那么只需创建此样式:
<style name="MyStyle">
<item name="colorAccent">@color/your_color</item>
</style>
然后将其作为主题应用于TextInputLayout
:
<android.support.design.widget.TextInputLayout
...
app:theme="@style/MyStyle" />
这基本上将主题(不是样式)设置为一个视图(而不是整个活动)。
答案 6 :(得分:4)
<style name="Widget.Design.TextInputLayout" parent="android:Widget">
<item name="hintTextAppearance">@style/TextAppearance.Design.Hint</item>
<item name="errorTextAppearance">@style/TextAppearance.Design.Error</item>
</style>
您可以覆盖此布局样式
您也可以更改内部的EditText项目样式。
答案 7 :(得分:4)
我使用了上述所有答案,但都没有奏效。此答案适用于 API 21+。文本字段聚焦时使用 app:hintTextColor 属性,其他状态下使用 app:textColorHint 属性。要更改底部线条颜色,请使用此属性 app:boxStrokeColor,如下所示:
<com.google.android.material.textfield.TextInputLayout
app:boxStrokeColor="@color/colorAccent"
app:hintTextColor="@color/colorAccent"
android:textColorHint="@android:color/darker_gray"
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
它也适用于 AutoCompleteTextView。希望它对你有用:)
答案 8 :(得分:4)
对我有用。如果您尝试使用带有标签的 EditText 并尝试更改为下划线颜色,请使用此选项。更改为 TextInputEditText 而不是 EditText。
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/editTextTextPersonName3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_16sdp"
android:layout_marginLeft="@dimen/_16sdp"
android:layout_marginTop="@dimen/_16sdp"
android:layout_marginEnd="@dimen/_8sdp"
android:layout_marginRight="@dimen/_8sdp"
android:textColorHint="@color/white"
app:layout_constraintEnd_toStartOf="@+id/guideline3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@color/white"
android:hint="Lastname"
android:textSize="@dimen/_14sdp"
android:inputType="textPersonName"
android:textColor="@color/white"
android:textColorHint="@color/white" />
</com.google.android.material.textfield.TextInputLayout>
答案 9 :(得分:2)
<style name="EditScreenTextInputLayoutStyle" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorControlNormal">@color/actionBar_background</item>
<item name="colorControlActivated">@color/actionBar_background</item>
<item name="colorControlHighlight">@color/actionBar_background</item>
<item name="colorAccent">@color/actionBar_background</item>
<item name="android:textColorHint">@color/actionBar_background</item>
</style>
将此样式应用于TextInputLayout
答案 10 :(得分:1)
更改 TextInputLayout + TextInputEditText 的线条颜色
<color name="mtrl_textinput_default_box_stroke_color" tools:override="true">@color/lineColor</color>
<style name="TextInputLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.Dense">
<item name="boxStrokeColor">@color/lineColor</item>
<item name="android:textColorHint">@color/hintColor</item>
<item name="boxStrokeWidth">1dp</item>
<item name="boxBackgroundColor">@color/backgroundColor</item>
</style>
<style name="TextInputEditText" parent="ThemeOverlay.MaterialComponents.TextInputEditText.FilledBox.Dense">
<item name="android:textColor">@color/black</item>
</style>
<com.google.android.material.textfield.TextInputLayout
style="@style/TextInputLayout"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<com.google.android.material.textfield.TextInputEditText
style="@style/TextInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
答案 11 :(得分:1)
您可以使用 TextInputLayout
中的 app:hintTextAppearance
属性更改 TextInputLayout
的标签颜色。
<android.support.design.widget.TextInputLayout
android:id="@+id/inputlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@style/TextAppearance.App.TextInputLayout">
<EditText
android:id="@+id/mail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="@string/email"
android:inputType="textEmailAddress"
android:maxLines="1"
android:paddingStart="5dp"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
风格:
<style name="TextAppearance.App.TextInputLayout"
parent="@android:style/TextAppearance">
<item name="android:textColor">@android:color/black</item>
<item name="android:textSize">20sp</item>
</style>
要更改 edittext
下划线颜色,您可以使用 android:backgroundTint
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Underline color change"
android:backgroundTint="@android:color/holo_red_light" />
答案 12 :(得分:0)
TextinputLayout
不是视图,而是Layout
,正如Dimitrios Tsigouris在his blog post here中的描述。因此,您不需要Style
(仅适用于Views
),而需要使用Theme
。在博客文章之后,我得到了以下解决方案:
从styles.xml
开始
<style name="TextInputLayoutAppearance" parent="Widget.Design.TextInputLayout">
<!-- reference our hint & error styles -->
<item name="android:textColor">@color/your_colour_here</item>
<item name="android:textColorHint">@color/your_colour_here</item>
<item name="colorControlNormal">@color/your_colour_here</item>
<item name="colorControlActivated">@color/your_colour_here</item>
<item name="colorControlHighlight">@color/your_colour_here</item>
</style>
然后在您的布局中添加
<com.google.android.material.textfield.TextInputLayout
android:theme="@style/TextInputLayoutAppearance"
...