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

EditText with custom theme shows underline under selection handle

我正在尝试通过应用主题来修改 EditText 的下划线颜色。

风格:

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

编辑文本:

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

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

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

您应该删除父属性。这仅是 API 23 台设备造成的。

<style name="MyTheme.EditText">
    <item name="colorControlActivated">@color/green</item>
</style>

您可以将此样式用作

<EditText 
   style="@style/MyTheme.EditText"/>

或者,您可以分离主题以引用 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"/>

好吧,但是这些下划线是从哪里来的?

android:theme 是 View 的属性,当您将样式设置为 android:theme 时,该样式将在 inflation 视图中由上下文主题的 ContextThemeWrapper 包装。

这意味着,如果您将 android:theme 属性 设置为包含 android:background 项的样式

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

此主题所有者的每个子视图都将具有绿色背景。

"Widget.AppCompat.EditText" 是一种样式并且引用 ?attr/editTextBackground 作为 "android:background"。并且在 v21/values-21.xml 文件中 @drawable/abc_edit_text_material 被定义为 editTextBackground.

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

我遇到了同样的问题。

我的布局在蓝色框内显示了一个 Edittext。在那里我想要一个白色的文本和一个白色的下划线和一个白色的选择控件。 我已经在我的 AppTheme 中设置了所有内容。一切顺利!

但我也有一个 Recyclerview 打破了蓝框。 RecyclerView 有包含 Edittext 的白卡。白色文本、下划线和控件在白色卡片中毫无意义。 :)

所以我尝试将文本、下划线和控件的颜色更改为深灰色。但没有成功。我在这个问题中遇到了与控件相同的问题。

我尝试使用已解决的答案,但这对我没有帮助。 也许是我的错,不知道。

所以我post这里是我的解决方案,连问题都已经回答了。

我在 styles.xml 里面添加了:

<style name="RecyclerEditText">
    <item name="colorAccent">@color/darkGray</item>
    <item name="colorControlNormal">@color/darkGray</item>
    <item name="colorControlActivated">@color/darkGray</item>
</style>

并在布局中 xml 我在 edittext 中添加:

android:textColor="@color/darkGray"
android:textColorHint="@color/darkGray"
android:theme="@style/RecyclerEditText"

--

现在控件图标没有偏移,也没有双下划线

非常感谢@litmon! 你让我想到了 "remove parent" 这个想法。