简体   繁体   English

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

[英]how to set cursor background transparent for EditText Android

I'm having this issue when I touch an EditText for changing text on Android: 当我触摸EditText以更改Android上的文本时,我遇到了这个问题:

在此输入图像描述
A white frame appears around the red cursor, and I need it to be transparent for showing properly the line of the EditText. 红色光标周围出现一个白框,我需要它是透明的,以便正确显示EditText的线条。 How do I change this? 我该如何改变?

code: 码:

<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"
            />

EDIT: I could fix it by replacing: 编辑:我可以通过替换:

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

for 对于

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

in styles.xml 在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>

在此输入图像描述

Sorry for the late, 对不起,我来晚了,
I think you have 我想你有
<item name="android:popupBackground">@color/white</item> in your style from v21, 你的风格中的<item name="android:popupBackground">@color/white</item>来自v21,
just remove it, It may solve your problem 只需删除它,它可以解决您的问题

更好地使用@android:color/transparentandroid:textCursorDrawable="@android:color/transparent"上使用@android:color/transparent而不是null android:textCursorDrawable="@android:color/transparent"

Add the following to your EditText XML instead of null ; 将以下内容添加到EditText XML而不是null ;

android:textCursorDrawable="@drawable/colorcursor"

Then, create a file called colorcursor.xml inside your drawable folder; 然后,在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>

Setting android:background to transparent didn't do the trick for me. android:background设置为透明对我来说不起作用。

In my case I set a theme to the parent viewgroup that defined the background to be a certain color, so it was applied to the EditText as well. 在我的例子中,我将一个主题设置为父视图组,该视图组将背景定义为某种颜色,因此它也应用于EditText。 Making sure that the background was just set for the parent solved it. 确保为父母设置的背景解决了它。

Another solution, which works if you are having this problem with an EditText inside a SearchView while using a Toolbar . 另一种解决方案,如果您在使用工具栏时在SearchView内部遇到此问题,则可以使用此解决方案。

  1. In your activity XML add the android:background="?attr/colorPrimary" attribute to Toolbar: 在您的活动XML中,将android:background="?attr/colorPrimary"属性添加到工具栏:

     <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. 然后,从styles.xml中的ToolBarStyle删除 <item name="android:background">@color/colorPrimary</item>

Hope this helps someone else looking for a solution for this particular case. 希望这可以帮助其他人为这个特定案例寻找解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM