如何更改我的EditText之一的文本选择句柄的颜色?

时间:2017-05-26 08:58:00

标签: android android-edittext textview android-theme android-styles

我的一些EditText放在蓝色背景上,对于这些EditText,我想为文本选择手柄设置一个白色(如果可能的话,不用上传棒子或更新的新png图像)

enter image description here

我尝试在自定义EditText的样式中添加<item name="android:colorAccent">#ffffffff</item>,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<resources>

  <!-- EditText on a blue background -->
  <style name="BlueBGTextView" parent="@android:style/Widget.Material.Light.EditText">
    <item name="android:background">@null</item> <!-- this will also remove all paddings -->  
    <item name="android:textCursorDrawable">@null</item> <!-- this to use android:textColor as the cursor color. -->  
    <item name="android:colorAccent">#ffffffff</item> 
  </style>

  <!-- default EditText -->
  <style name="AppTheme.EditTextStyle" parent="@android:style/Widget.Material.Light.EditText">
    <item name="android:background">@null</item> <!-- this will also remove all paddings -->  
  </style>

  <!-- default Theme -->
  <style name="AppTheme" parent="@android:style/Theme.Material.Light.NoActionBar"> 
    <item name="android:editTextStyle">@style/AppTheme.EditTextStyle</item>
    <item name="android:colorAccent">#ff0288d1</item> <!-- use to setup the color of the text selection handles -->   
    <item name="@attr/BlueBGTextViewStyle">@style/BlueBGTextView</item>
  </style>

</resources>

但它不起作用:(我错过了什么?

2 个答案:

答案 0 :(得分:6)

您可以在xml中指定textColorHighlight属性

样品

android:textColorHighlight="#ff0000"

要更改手柄,请使用colorControlActivated属性

定义样式
<style name="BlueBGTextView">
    <item name="colorControlActivated">@android:color/black</item>
</style>

并将其设置为EditText

的主题
<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/BlueBGTextView"/>

答案 1 :(得分:0)

当您使用 Theme.MaterialComponents 作为应用主题时,手柄颜色将遵循您的主要颜色

还要注意 theme.xml 中的 Light/Dark 模式和您的测试手机上的应匹配。

如果您的手机在深色模式下运行,则为 colorPrimary 添加 values-night/theme.xml 项,否则为 values/themes.xml 添加

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

    <item name="colorPrimary">@color/colorAccent</item>

</style>
相关问题