[英]How to set color of selection in EditText in my custom theme? (android)
[英]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上:
<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"/>
基本上它可以工作,但是當我嘗試選擇或移動光標時,選擇手柄也有下划線。 您可以在屏幕截圖中看到這一點。
有人知道如何解決這個問題嗎?
您可以將此樣式用作
<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 like
<item name="android:background">@color/green</item>
該主題所有者的每個兒童視圖都將具有綠色背景。
"Widget.AppCompat.EditText"
是一種樣式, "Widget.AppCompat.EditText"
?attr/editTextBackground
"android:background"
引用為"android:background"
。 在v21 / values-21.xml文件中, @drawable/abc_edit_text_material
定義為editTextBackground
。
因此,對於您的示例, @drawable/abc_edit_text_material
將成為EditText和SelectionHandlers的背景。
您應該刪除父屬性。 這只是API 23設備引起的。
<style name="MyTheme.EditText">
<item name="colorControlActivated">@color/green</item>
</style>
我遇到過同樣的問題。
我的布局在藍色框內顯示了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! 你把我推到了“移除父母”的想法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.