具有自定义主题的EditText在选择句柄下显示下划线

Bau*_*kel 3 android android-theme android-edittext

我试图通过应用主题修改EditText的下划线颜色.

样式:

<style name="MyTheme.EditText" parent="Widget.AppCompat.EditText">
    <item name="colorControlActivated">@color/green</item>
</style>
Run Code Online (Sandbox Code Playgroud)

EditText上:

<EditText
    android:id="@+id/editText_amount"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint_enter_amount"
    android:theme="@style/MyTheme.EditText"/>
Run Code Online (Sandbox Code Playgroud)

基本上它可以工作,但是当我尝试选择或移动光标时,选择手柄也有下划线.您可以在屏幕截图中看到这一点.

截图

有人知道如何解决这个问题吗?

Tol*_*kur 7

您可以将此样式用作

<EditText 
   style="@style/MyTheme.EditText"/>
Run Code Online (Sandbox Code Playgroud)

或者,您可以分离主题以引用editTextStyle属性.

<style name="MyEditTextStyle" parent="Widget.AppCompat.EditText">
    <item name="colorControlActivated">@color/green</item>
</style>

<style name="MyTheme.EditText">
    <item name="editTextStyle">@style/MyEditTextStyle</item>
</style>

<EditText
    android:theme="@style/MyTheme.EditText"/>
Run Code Online (Sandbox Code Playgroud)

好吧,但这些下划线来自哪里?

android:theme是View的一个属性,当您将样式设置android:theme为时,该样式将由ContextThemeWrapper包含上下文主题,同时处于视图的膨胀状态.

这意味着,如果你设置android:theme包含android:background类似项的样式的属性

<item name="android:background">@color/green</item>
Run Code Online (Sandbox Code Playgroud)

该主题所有者的每个儿童视图都将具有绿色背景.

"Widget.AppCompat.EditText"是一种风格和参考?attr/editTextBackground作为"android:background".并在v21/values-21.xml文件@drawable/abc_edit_text_material中定义为editTextBackground.

因此,对于您的示例,@drawable/abc_edit_text_material将成为EditText和SelectionHandlers的背景.