【问题标题】:EditText line is not visible and does not change colourEditText 行不可见且不改变颜色
【发布时间】:2017-06-09 03:04:22
【问题描述】:

所以一段时间以来,我一直在尝试解决我在 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>

Emulator Output

我的styles.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>

我的 colors.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>

【问题讨论】:

  • 代码没有问题,添加你的主题代码。
  • @W4R10CK 我已经添加了
  • 尝试从预览部分更改主题。
  • 可以添加colors.xml文件吗?
  • @luian Popescu 我已经添加了文件,请看一下

标签: android colors android-edittext styles output


【解决方案1】:

在您的 xml 布局文件中将 backgroundTintwhite 更改为其他深色 edittext 并更改活动的主题

请参考此链接:http://www.codexpedia.com/android/setting-edittext-underline-color-in-android/

【讨论】:

  • 任何反对投票的理由
  • 你检查过这个
  • 不适用于 android.support.design.widget.TextInputLayout
  • 我知道你在谈论 edittext,☺☺☺ 否决票的原因......(不是我)
【解决方案2】:
<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>

【讨论】:

  • 如果我有多个 EditText,我会在每个之后都有图层列表吗,例如EditText、LayerList、Edit、Text、LayerList 还是将 LayerList 定义在 XML 页面的底部?
  • 在res文件夹内的drawable文件夹内创建edit_text_bottom_border.xml文件并设置edittext的背景,布局文件内无需编写layer-list代码。
【解决方案3】:

将高度更改为 wrap_content

<EditText
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:hint="Name"
  android:layout_margin="16dp"/>

【讨论】:

  • 将高度更改为wrap_content 并没有做任何新的事情,它已经match_parent 与父高度匹配。
  • @W4R10CK 没错,我的高度已经是 wrap_conent 和宽度是 match_parent 意思是应该水平显示以匹配线性布局,但没有显示
【解决方案4】:

从您的布局文件中删除以下行(如果已存在)

android:backgroundTint="@color/white"

【讨论】:

    【解决方案5】:
    <?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>
    

    【讨论】:

    • 不幸的是,这对我没有任何改变。
    • 那么你能告诉我,到底发生了什么吗?
    • @diwn.dhanvani 正如我上面解释的那样,应该在 EditText 字段下方的行没有显示
    • 试试这个:
    • 我已经编辑了答案。所以你可以试试这个。在我的应用程序中,这在提示文本下方显示了一条红线。 @MichaelDybek
    猜你喜欢
    • 2021-08-30
    • 1970-01-01
    • 2021-07-09
    • 2020-02-07
    • 1970-01-01
    • 2012-03-03
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多