【问题标题】:EditText with custom theme shows underline under selection handle具有自定义主题的 EditText 在选择句柄下显示下划线
【发布时间】:2016-06-02 09:37:05
【问题描述】:

我正在尝试通过应用主题来修改 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"/>

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

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

【问题讨论】:

    标签: android android-edittext android-theme


    【解决方案1】:

    您可以将这种样式用作

    <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 时,该样式将由 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 的背景。

    【讨论】:

      【解决方案2】:

      您应该删除父属性。这仅由 API 23 设备引起。

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

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题。

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

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

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

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

        所以我在这里发布我的解决方案,即使问题已经回答了。

        在我添加的 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! 你把我推向了“移除父级”的想法。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-08-22
          相关资源
          最近更新 更多