如何更改 android 中的编辑文本底线颜色?
how to change the edit text bottom line color in android?
Hi I am doing a login page for my project whose xml looks like this
<EditText
android:id="@+id/user_name_id"
android:layout_height="wrap_content"
android:layout_width="300dp"
android:ems="10"
android:singleLine="true"
android:layout_gravity="top|center"
android:gravity="center"
android:layout_marginTop="200dp"
android:hint=" Email Address"
android:textColor="#B3B3B3"
android:textColorHint="#B3B3B3"
android:textCursorDrawable="@drawable/color_cursor"
/>
关于代码显示文本、提示和光标在#b3b3b3 颜色代码中。我想要底线也使用相同的颜色代码。我也尝试使用选择器,如下所示。但对我来说很有效。请提供一个真正的方法来特别改变edittext的底线颜色。
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_pressed="true"
android:drawable="@android:drawable/edittext_pressed"
/> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="@drawable/edittext_focused_blue"
/> <!-- focused -->
<item
android:drawable="@android:drawable/edittext_normal"
/> <!-- default -->
</selector>
并且我在 xml 中使用了以下代码来更改光标颜色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<size android:width="1dp" />
<solid android:color="#B3B3B3" />
</shape>
否则任何人都可以为我修改下面的代码
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
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. -->
</style>
</resources>
如果您使用的是 v21+ (lollipo) 支持库,您可以在应用的主题中设置 colorAccent:
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
...
<item name=”colorAccent”>@color/accent</item>
...
</style>
使用 app-compact 库
<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>
现在,您的应用中的 Edittext 线条颜色会发生变化。
我使用的是 api 21 级(棒棒糖),我找到了解决方案,因为该解决方案仅适用于 api 21 级及以上。使用图层列表
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="line" >
<solid android:color="#B3B3B3" />
</shape>
</item>
<item android:top="-2dp" android:right="-2dp" android:left="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="0.1dp"
android:color="#B3B3B3" />
</shape>
</item>
</layer-list>
如果用于 api 级别低于 21 则它给出异常。
Hi I am doing a login page for my project whose xml looks like this
<EditText
android:id="@+id/user_name_id"
android:layout_height="wrap_content"
android:layout_width="300dp"
android:ems="10"
android:singleLine="true"
android:layout_gravity="top|center"
android:gravity="center"
android:layout_marginTop="200dp"
android:hint=" Email Address"
android:textColor="#B3B3B3"
android:textColorHint="#B3B3B3"
android:textCursorDrawable="@drawable/color_cursor"
/>
关于代码显示文本、提示和光标在#b3b3b3 颜色代码中。我想要底线也使用相同的颜色代码。我也尝试使用选择器,如下所示。但对我来说很有效。请提供一个真正的方法来特别改变edittext的底线颜色。
<selector
xmlns:android="http://schemas.android.com/apk/res/android"
>
<item
android:state_pressed="true"
android:drawable="@android:drawable/edittext_pressed"
/> <!-- pressed -->
<item
android:state_focused="true"
android:drawable="@drawable/edittext_focused_blue"
/> <!-- focused -->
<item
android:drawable="@android:drawable/edittext_normal"
/> <!-- default -->
</selector>
并且我在 xml 中使用了以下代码来更改光标颜色
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<size android:width="1dp" />
<solid android:color="#B3B3B3" />
</shape>
否则任何人都可以为我修改下面的代码
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
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. -->
</style>
</resources>
如果您使用的是 v21+ (lollipo) 支持库,您可以在应用的主题中设置 colorAccent:
<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
...
<item name=”colorAccent”>@color/accent</item>
...
</style>
使用 app-compact 库
<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>
现在,您的应用中的 Edittext 线条颜色会发生变化。
我使用的是 api 21 级(棒棒糖),我找到了解决方案,因为该解决方案仅适用于 api 21 级及以上。使用图层列表
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="line" >
<solid android:color="#B3B3B3" />
</shape>
</item>
<item android:top="-2dp" android:right="-2dp" android:left="-2dp">
<shape>
<solid android:color="@android:color/transparent" />
<stroke
android:width="0.1dp"
android:color="#B3B3B3" />
</shape>
</item>
</layer-list>
如果用于 api 级别低于 21 则它给出异常。