问题
My problem is that I don't know how to change underline color of EditText - I have LG Nexus 5 (Android 6).
I would like to change normal underline color to white. I used all params like (style):
- colorControlNormal
- colorControlActivated
- colorControlHighlight
- textColorSecondary
- colorPrimary
- colorPrimaryDark
e.g.
<style name="FormFont" parent="@android:style/TextAppearance.Medium">
<item name="android:textColor">@color/white</item>
<item name="android:textSize">@dimen/form_text_size</item>
<item name="colorControlNormal">#c5c5c5</item>
</style>
but nothing seems to work (it only changes color when EditText us focused like in above pic).
回答1:
<style name="Theme.App.Base" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorControlNormal">#c5c5c5</item>
<item name="colorControlActivated">@color/accent</item>
<item name="colorControlHighlight">@color/accent</item>
</style>
回答2:
The most efficient thing to do is to add the colorAccent attribute in your AppTheme style like this:
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorAccent">@color/colorAccent</item>
<item name="android:editTextStyle">@style/EditTextStyle</item>
</style>
<style name="EditTextStyle" parent="Widget.AppCompat.EditText"/>
The colorAccent attribute is used for widget tinting throughout the app and thus should be used for consistency
回答3:
To make it work in Android 6, create your custom theme in
res/values-v21/styles.xml
.
<style name="FormFont" parent="@android:style/TextAppearance.Medium">
<item name="colorControlNormal">#c5c5c5</item>
</style>
<style name="FormFontWhite" parent="@android:style/TextAppearance.Medium">
<item name="colorControlNormal">#ffffff</item>
</style>
and apply then in your EditText
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/FormFont"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/FormFontWhite"/>
For more info check @reVerse answer .
Hope this helps!
回答4:
Try using android:backgroundTint="#yourUnderlineColor"
.
Only available for API 21 and higher.
回答5:
<android.support.v7.widget.AppCompatEditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/cux_label_password"
android:inputType="textPassword"
app:theme="@style/editTextStyle" />
<style name="editTextStyle" parent="TextAppearance.AppCompat">
<item name="colorControlNormal">#FFFFFF</item>
<item name="colorControlActivated">#FFFFFF</item>
<item name="colorControlHighlight">#FFFFFF</item>
</style>
来源:https://stackoverflow.com/questions/35756757/material-edittext-underline-color