問題描述
我正在嘗試通過應用主題來修改 EditText 的下劃線顏色.
i am trying to modify the underline color of a EditText by applying a theme.
風格:
<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"/>
基本上它可以工作,但是當我嘗試選擇或移動光標時,選擇句柄也會加下劃線.您可以在屏幕截圖中看到這一點.
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?
推薦答案
你可以把這個樣式當作一個
You can use this style as a
<EditText
style="@style/MyTheme.EditText"/>
或者,您可以將主題分開以引用 editTextStyle 屬性.
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"/>
好的,但是這些下劃線是從哪里來的?
android:theme
是 View 的一個屬性,當您將樣式設置為 android:theme
時,該樣式將由具有上下文主題的 ContextThemeWrapper 包裝,同時膨脹查看.
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.
這意味著,如果您將 android:theme
屬性設置為包含 android:background
項目的樣式
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"
是一種樣式,并將 ?attr/editTextBackground
引用為 "android:background"
.而在 v21/values-21.xml 文件中,@drawable/abc_edit_text_material
被定義為 editTextBackground
.
"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
.
因此,對于您的示例,@drawable/abc_edit_text_material
成為您的 EditText 和 SelectionHandlers 的背景.
So, for your example, @drawable/abc_edit_text_material
becomes a background of your EditText and SelectionHandlers.
這篇關于具有自定義主題的 EditText 在選擇句柄下顯示下劃線的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!