如何为Android中的EditText设置光标背景透明

10

当我在Android上触摸EditText以更改文本时,出现了以下问题:

enter image description here
一个白色框架出现在红色光标周围,我需要它是透明的,以便正确显示EditText的行。我该如何更改这个设置?

代码:

<EditText
            android:id="@+id/login_user_name_text"
            android:textCursorDrawable="@null"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:inputType="textEmailAddress"
            android:imeOptions="actionNext"
            android:maxLines="1"
            android:hint="@string/email"
            android:maxLength="60"
            />

编辑:我可以通过替换以下内容来解决它:

<item name="android:background">@color/white</item>

对于

<item name="android:background">@color/transparent</item>

在 styles.xml 文件中。
 <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDisablePreview">true</item>
        <item name="android:background">@color/transparent</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    </style>

enter image description here


1
光标的图像本身是否有白色背景?您可能需要编辑该图像。 - aconnelly
属性名是什么或者在哪里编辑???我没有为我的EditText设置光标图像的任何值,我将上传我的代码。 - Jesús Ayala
5个回答

4

很抱歉回复晚了,
我认为你在v21样式中有
<item name="android:popupBackground">@color/white</item>
只需将其删除,可能可以解决你的问题。


3

如果您在使用工具栏时,遇到了EditText位于SearchView中的问题,可以采用另一种解决方案。

  1. In your activity XML add the android:background="?attr/colorPrimary" attribute to Toolbar:

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:theme="@style/ToolBarStyle"
    android:background="?attr/colorPrimary"
    android:elevation="2dp" />
    
  2. Then, remove <item name="android:background">@color/colorPrimary</item> from your ToolBarStyle in styles.xml.

希望这篇解决方案能帮助到其他遇到类似问题的人。

能否请下投票者解释一下为什么要点踩?谢谢! - Igor
遇到了类似的问题。但在我的情况下,问题很简单,只需从工具栏布局中删除 android:background="#FFFFFF" 即可解决。 不过你指出了问题,谢谢 :) - ice_chrysler

1

android:textCursorDrawable="@android:color/transparent" 中最好使用 @android:color/transparent 而不是 null


1

android:background设置为透明对我没有起作用。

在我的情况下,我为父视图组设置了一个主题,定义了背景为某种颜色,因此它也应用于EditText。确保仅为父级设置背景解决了这个问题。


0
请将以下内容添加到您的EditText XML中,而不是null

android:textCursorDrawable="@drawable/colorcursor"

接下来,在您的drawable文件夹中创建一个名为colorcursor.xml的文件;

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size android:width="3dp" />
    <solid android:color="#FFFFFF"  />
</shape>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接