android - 扩展Widget的样式继承

标签 android android-custom-view android-theme android-5.0-lollipop

在我的项目(支持 AppCompat 的 Target API 21)中,我需要扩展 EditText 类。我的问题是 MyEditText 类不继承 EditText 自定义样式:

<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绿色


截图:

enter image description here

  • 第 1 行:EditText 重点
  • 第 2 行:EditText 未聚焦(启用)
  • 第 3 行:MyEditText 未聚焦(启用)

我的问题是:如何在 MyEditText 中继承默认的 EditText 样式?

最佳答案

有一种方法非常、非常、非常简单。

与其从 android.widget.EditText 继承您的 Custom EditText,不如继承此类并查看它是否有效:

android.support.v7.internal.widget.TintEditText

类(class)链接:TintEditText.java

阅读类(class)的 Javadoc,他说:

/**
 * 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.
 */

在本页的延伸部分(Using Support Library APIs)有一个警告:当使用支持库中的类时,请确保从适当的包中导入类。例如,在应用 ActionBar 类时:

  • 使用支持库时的 android.support.v7.app.ActionBar。
  • 仅针对 API 级别 11 或更高级别开发时的 android.app.ActionBar。

我是这样解释的,也就是说,如果你在使用支持库,总是尝试导入或继承合适的包。 (没有什么比写的大声笑。:D)

关于android - 扩展Widget的样式继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27026826/

相关文章:

android - Flutter 发布 APK :Invalid keystore format with Linux(deepin)

java - 如何在改造界面中从 strings.xml 中获取值?

android - 将视频保存为输入而不是 android 中的相机

android - 从属首选项在禁用时显示错误的颜色字体

android - 更改 ActionBar 和 SearchView 背景

android - 在 ListView 中更新 SeekBar

java - 如何在 Android 中更改 Canvas 上颜料的颜色/厚度?

java - 自定义View如何访问变量?

android - 自定义 Android 日历 View

android - 为什么安卓:buttonStyle does not style buttons?