EditText with custom theme shows underline under selection handle

橙三吉。 提交于 2019-11-27 07:15:29

问题


i am trying to modify the underline color of a EditText by applying a theme.

Style:

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

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

Basically it works, but when i try to select or move the cursor the selection handle is also underlined. You can see this in the screenshot.

Does someone know how to fix this?


回答1:


You can use this style as a

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

Or, you can separate your theme for referencing the editTextStyle attribute.

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

Alright, but where are these underlines come from?

android:theme is an attribute of View and when you set a style as android:theme, that style will be wrapped by ContextThemeWrapper with context theme while in inflation of view.

So that means, if you set android:theme property with style that contains android:background item like

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

every child view of this theme owner will be have a green background.

"Widget.AppCompat.EditText" is a style and references ?attr/editTextBackground as a "android:background". And in v21/values-21.xml file @drawable/abc_edit_text_material is defined as editTextBackground.

So, for your example, @drawable/abc_edit_text_material becomes a background of your EditText and SelectionHandlers.




回答2:


You should remove parent attribute. This is caused only API 23 device.

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



回答3:


I had the same issue.

My layout shows a Edittext inside a blue box. There i wanted to have a white text and a white underline and a white selection control. I've set all in my AppTheme. All fine!

But i also had a Recyclerview blow the blue box. The RecyclerView has white cards that contains Edittext. White text, underline and control makes no sense in white cards. :)

So i tried to change the color of the text, underline and controls to dark gray. But with no success. I had the same issues with the control like in this question.

I tried using the solved answer, but this didn't helped me. Maybe my fault, don't know.

So i post here my solution, even the question already answered.

Inside the styles.xml i added:

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

and in the layout xml i added inside edittext:

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

--

Now i have no offset in control icon, and no double underline.

Thanks a lot @litmon ! You pushed me to the "remove parent" idea.



来源:https://stackoverflow.com/questions/37588019/edittext-with-custom-theme-shows-underline-under-selection-handle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!