我正在使用android设计库的TextinputLayout
。但是,不能自定义提示颜色,标签颜色的下划线颜色EditText
里面TextinputLayout
。请帮忙。
我正在使用android设计库的TextinputLayout
。但是,不能自定义提示颜色,标签颜色的下划线颜色EditText
里面TextinputLayout
。请帮忙。
Answers:
<item name="colorControlNormal">#c5c5c5</item>
<item name="colorControlActivated">@color/accent</item>
<item name="colorControlHighlight">@color/accent</item>
有关更多信息,请检查此线程。
<style name="MyHintStyle" parent="@android:style/TextAppearance">
<item name="android:textColor">@color/main_color</item>
</style>
并像这样使用它:
<android.support.design.widget.TextInputLayout
...
app:hintTextAppearance="@style/MyHintStyle">
<android.support.design.widget.TextInputLayout
...
app:hintTextAppearance="@style/MyHintStyle"
android:textColorHint="#c1c2c4">
感谢@AlbAtNf
android:textColorHint
为TextInputLayout
XML。
根据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>
重点:
没有重点:
错误信息:
借助材料组件库,您可以使用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"
...>
boxStrokeColor
。在该示例中selector_stroke_color
,尤其是最后一行:<item android:alpha="0.38" android:color="?attr/colorOnSurface"/>
本博客文章介绍的各种造型方面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));
}
}
});
如果你想改变酒吧/线条颜色提示文本颜色的TextInputLayout
(什么口音的颜色通常是),然后就创建这个风格:
<style name="MyStyle">
<item name="colorAccent">@color/your_color</item>
</style>
然后将其TextInputLayout
作为主题应用:
<android.support.design.widget.TextInputLayout
...
app:theme="@style/MyStyle" />
这基本上将一个主题(不是样式)设置为一个视图(而不是整个活动)。
<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-item样式。
<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
android:theme="@style/EditScreenTextInputLayoutStyle"
正如Dimitrios Tsigouris在他的博客文章中很好地描述TextinputLayout
的Layout
,A 不是观点,而是a 。因此,您不需要仅用于的,而使用。在博客文章之后,我得到了以下解决方案: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"
...