如何更改Android中EditText的底线颜色?
问题描述:
我试过改变我的编辑文本底部颜色各种方式,但它不会改变。我不想使用drawable。如何更改Android中EditText的底线颜色?
这里是我的XML和我尝试使用的方法。
方法之一:
android:backgroundTint="@color/colorPrimary"
方法二:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="colorControlNormal">@color/colorPrimary</item>
<item name="colorControlActivated">@color/colorPrimary</item>
<item name="colorControlHighlight">@color/colorPrimary</item>
</style>
</resources>
这里是我的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res /android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/b"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.isaac.loginexamples.b">
<RelativeLayout
android:background="#009688"
android:layout_width="match_parent"
android:layout_height="@dimen/relative_half_screen_height">
</RelativeLayout>
<android.support.v7.widget.CardView
card_view:cardElevation="18dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_width="@dimen/card_view_width"
android:layout_height="@dimen/card_view_height">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="@+id/header_logo_two"
android:background="#22313F"
android:layout_width="match_parent"
android:layout_height="@dimen/view_header_height"></View>
<View
android:background="@drawable/shadow"
android:layout_below="@+id/header_logo_two"
android:layout_width="match_parent"
android:layout_height="7dp"></View>
<ImageView
android:layout_marginTop="25dp"
android:layout_centerHorizontal="true"
android:id="@+id/logo_two"
android:background="@mipmap/ic_launcher"
android:layout_width="@dimen/logo_image_width_height"
android:layout_height="@dimen/logo_image_width_height" />
<EditText
android:backgroundTint="@color/colorPrimary"
android:id="@+id/username"
android:layout_marginTop="15dp"
android:layout_centerHorizontal="true"
android:layout_below="@+id/logo_two"
android:layout_width="270dp"
android:layout_height="45dp"
android:inputType="textEmailAddress" />
<EditText
android:layout_marginTop="8dp"
android:layout_alignLeft="@+id/username"
android:layout_below="@+id/username"
android:layout_width="240dp"
android:layout_height="45dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
我已经试过,我已经看到了这两种方法在线,但它不”工作。我正在运行最新版本的Android Studio。我希望有一个解决方案。 注意:我不想使用可绘制的。 非常感谢!
答
如果你不想根据您的活动或片段中的EditText
<View
android:id="@+id/username_view"
android:layout_width="match_parent"
android:layout_height="@dimen/size_1"
android:layout_alignEnd="@+id/username"
android:layout_alignLeft="@+id/username"
android:layout_alignRight="@+id/username"
android:layout_alignStart="@+id/username"
android:layout_below="@+id/username"
android:background="@color/grey"/>
的focuschange使用可绘制,您可以使用“查看”下面的EditText和更改View
的颜色,添加这些行代码,照顾基于用户焦点变化的视图颜色变化
@OnFocusChange(R.id.username)
void onUserNameFocusChanged(View v, boolean hasFocus) {
if (userNameView != null) {
int focusViewColor = hasFocus ? getResources().getColor(R.color.red) : getResources().getColor(R.color.grey);
userNameView.setBackgroundColor(focusViewColor);
}
}
+0
@ Tanis.7x感谢您的编辑。 – Madhu
也许你应该使用可绘制的xD – Beloo
马ybe但你知道,我宁愿使用AppCompat。我认为它更好,更可靠。 @Beloo –