簡體   English   中英

如何更改我的 EditText 之一的文本選擇句柄的顏色?

[英]How to change the color of the text selection handle of one of my EditText?

我的一些 EditText 放置在藍色背景上,對於這些 EditText,我想為文本選擇句柄設置白色(如果可能,在棒棒糖或更新版本上不上傳新的 png 圖像)

在此處輸入圖片說明

我嘗試在我的自定義 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>

但這不起作用:(我錯過了什么?

您可以在 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"/>

當您使用 Theme.MaterialComponents 作為 App Theme 時,手柄顏色將遵循您的Primary color

還要注意 theme.xml 中的 Light/Dark 模式和您的測試手機上的應匹配。

如果您的手機在暗模式下運行,則為values-night/theme.xml添加colorPrimary項目,否則為values/themes.xml添加

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

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

</style>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM