扩展小部件的样式继承 [英] Style inheritance of extended Widget
问题描述
在我的项目(具有AppCompat支持的Target API 21)中,我需要扩展EditText
类.我的问题是MyEditText
类不能继承使用以下方式自定义的EditText
样式:
In my project (Target API 21 with AppCompat support), I need to extend the EditText
class. My problem is MyEditText
class does not inherit EditText
style customized with:
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar" >
<item name="colorPrimary">@color/primary</item>
<item name="colorControlNormal">@color/grey_light</item>
<item name="colorControlActivated">@color/primary</item>
<item name="colorControlHighlight">@color/primary</item>
</style>
带有@color/primary
绿色
屏幕截图:
- 第1行:
EditText
专注 - 第2行:
EditText
未聚焦(启用) - 第3行:
MyEditText
未聚焦(启用)
- line 1:
EditText
focused - line 2:
EditText
not focused (enabled) - line 3:
MyEditText
not focused (enabled)
我的问题是:如何继承MyEditText
中的默认EditText
样式?
My question is: How can I inherit default EditText
style in MyEditText
?
推荐答案
有一种非常非常非常简单的方法.
Has a way very, very, very easy.
而不是从android.widget.EditText继承自定义EditText,而是继承此类并查看其是否有效:
Instead of inheriting from android.widget.EditText your Custom EditText, inherit this class and see if it works:
android.support.v7.internal.widget.TintEditText
链接到课程:阅读该课程的Javadoc,他说:
Read the Javadoc of the class, he says:
/**
* An tint aware {@link android.widget.EditText}.
* <p>
* This will automatically be used when you use {@link android.widget.EditText} in your
* layouts. You should only need to manually use this class when writing custom views.
*/
在此页面中,(使用支持库API)具有警告:在使用支持库中的类时,请确保您从适当的程序包中导入了该类.例如,在应用ActionBar类时:
In the stretch of this page (Using Support Library APIs) has a caution which is: When using classes from the Support Library, be certain you import the class from the appropriate package. For example, when applying the ActionBar class:
-
使用支持库时
- android.support.v7.app.ActionBar.
- 仅针对API级别11或更高级别进行开发时的
- android.app.ActionBar.
我对此进行了解释,也就是说,如果您使用的是支持库,请始终尝试导入或继承适当的程序包. (没什么比这写的大声笑:D)
I interpreted this, that is, if you are using a support library, always try to import or inherit the appropriate package. (Nothing less than that is written lol.: D)
这篇关于扩展小部件的样式继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!