所以一段时间以来,我一直试图解决我在 EditText 字段上遇到的问题,因为它没有显示提示或实际文本下方的单行。通常,默认情况下该行为黑色,所以我的问题是这个;如何使线条再次显示,以及如何更改线条的颜色?(我假设android:backgroundHint可以解决问题,但是我根本看不到该行,因此无法对其进行测试)。我正在使用 API 24/25。
下面是我的 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"
android:id="@+id/activity_register"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.michael.whatsupldn.RegisterActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:layout_margin="16dp"/>
</LinearLayout>
</RelativeLayout>
仿真器输出
我的样式.xml代码
<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>
</style>
<style name="EditTextTheme">
<!-- Used for the bottom line when not selected / focused -->
<item name="colorControlNormal">#FAFAFA</item>
<!-- colorControlActivated & colorControlHighlight use the colorAccent color by default -->
</style>
</resources>
我的颜色.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="colorRadioButton">#f5f5f5</color>
<color name="yellow1">#ffff00</color>
<color name="yellow2">#ffEA00</color>
<color name="grey05">#F5F5F5</color>
<color name="orange4">#FFB74D</color>
<color name="orange5">#FFA726</color>
<color name="blue2">#f5f5f5</color>
<color name="blue25">#fafafa</color>
<color name="white">#ffffff</color>
</resources>
<EditText
android:id="@+id/editTextAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20dp"
android:inputType="textMultiLine|textCapWords"
android:background="@drawable/edit_text_bottom_border"
android:hint="Address">
// edit_text_bottom_border.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/r...">
<item
android:bottom="1dp"
android:left="-3dp"
android:right="-3dp"
android:top="-3dp">
<shape android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#000" />
<solid android:color="#00ffffff" />
</shape>
</item>
</layer-list>
xml 布局文件中edittext
中的backgroundTint
从白色更改为其他深色,并更改活动的主题
请参考此链接:http://www.codexpedia.com/android/setting-edittext-underline-color-in-android/
将高度更改为wrap_content
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:layout_margin="16dp"/>
从布局文件中删除以下行(如果已经存在)
android:backgroundTint="@color/white"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_register"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.michael.whatsupldn.RegisterActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Underline color change"
android:backgroundTint="@android:color/holo_red_light" />
<requestFocus/>
</EditText>
</LinearLayout>
</RelativeLayout>