問(wèn)題描述
我正在嘗試通過(guò)應(yīng)用主題來(lái)修改 EditText 的下劃線顏色.
i am trying to modify the underline color of a EditText by applying a theme.
風(fēng)格:
<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"/>
基本上它可以工作,但是當(dāng)我嘗試選擇或移動(dòng)光標(biāo)時(shí),選擇句柄也會(huì)加下劃線.您可以在屏幕截圖中看到這一點(diǎn).
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.
有人知道如何解決這個(gè)問(wèn)題嗎?
Does someone know how to fix this?
推薦答案
你可以把這個(gè)樣式當(dāng)作一個(gè)
You can use this style as a
<EditText
style="@style/MyTheme.EditText"/>
或者,您可以將主題分開(kāi)以引用 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"/>
好的,但是這些下劃線是從哪里來(lái)的?
android:theme
是 View 的一個(gè)屬性,當(dāng)您將樣式設(shè)置為 android:theme
時(shí),該樣式將由具有上下文主題的 ContextThemeWrapper 包裝,同時(shí)膨脹查看.
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
屬性設(shè)置為包含 android:background
項(xiàng)目的樣式
So that means, if you set android:theme
property with style that contains android:background
item like
<item name="android:background">@color/green</item>
此主題所有者的每個(gè)子視圖都將具有綠色背景.
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
.
因此,對(duì)于您的示例,@drawable/abc_edit_text_material
成為您的 EditText 和 SelectionHandlers 的背景.
So, for your example, @drawable/abc_edit_text_material
becomes a background of your EditText and SelectionHandlers.
這篇關(guān)于具有自定義主題的 EditText 在選擇句柄下顯示下劃線的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!