扩展小部件的样式继承 [英] Style inheritance of extended Widget

查看:108
本文介绍了扩展小部件的样式继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目(具有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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆