【问题标题】:Style inheritance of extended Widget扩展Widget的样式继承
【发布时间】:2014-11-19 20:57:55
【问题描述】:

在我的项目中(支持 AppCompat 的目标 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绿色


截图:

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

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

【问题讨论】:

  • AppCompat 使用自定义 LayoutInflater,当它看到 EditText 元素时,它会替换它自己的 tint-aware EditText 实现。不幸的是,这个类不是公开的,也不是制作你自己的色彩感知自定义 EditText 所需的类,所以你现在能做的最好的就是在 AOSP issue tracker 上针对 appcompat 提交功能请求
  • 谢谢你的帮助,我开了票code.google.com/p/android/issues/detail?id=80114

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


【解决方案1】:

有一个非常、非常、非常简单的方法。

不要从 android.widget.EditText 继承你的自定义 EditText,而是继承这个类,看看它是否有效:

android.support.v7.internal.widget.TintEditText

课程链接:TintEditText.java

阅读该课程的 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。
  • android.app.ActionBar 仅适用于 API 级别 11 或更高级别的开发。

我对此进行了解释,也就是说,如果您正在使用支持库,请始终尝试导入或继承适当的包。 (无非就是这样写的,哈哈。:D)

【讨论】:

  • 解决方案简单而完美,当然最适合这个问题。 +1 =D
  • 感谢您的回答
【解决方案2】:

您可以使用样式来解决它

在你的主题中

<style name="AppTheme.BaseNoActionBar" parent="Theme.AppCompat.Light">
    <item name="android:editTextStyle">@style/Widget.EditText</item>
</style>


<!-- EditText style -->
<style name="Widget.EditText" parent="Widget.AppCompat.EditText">
    <item name="android:textCursorDrawable">@drawable/cursor_gray</item>
    <item name="android:background">@drawable/underline_gray</item>
</style>

然后定义光标

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <size android:width="1dp" />
    <solid android:color="@color/lightGrayLabel"  />
</shape>

以及根据这个hack https://stackoverflow.com/a/20891131的drawable

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="-6dp" android:left="-6dp" android:right="-6dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent"/>
            <stroke android:color="@color/lightGrayLabel" android:width="3dp"/>
        </shape>
    </item>
</layer-list>

我也尝试使用选择器作为“背景”字段,但它不适用于“android:state_selected”或“android:state_activated”

这是一个小例子

【讨论】:

  • 谢谢,我终于做到了。
猜你喜欢
  • 2017-10-19
  • 2011-04-06
  • 2017-05-18
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多