EditText 的子类看起来与 Android 4 上的普通 EditText 不同 [英] Child class of EditText looks different than normal EditText on Android 4

查看:20
本文介绍了EditText 的子类看起来与 Android 4 上的普通 EditText 不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在开发真实应用程序时发现的错误",但我创建了一个空白项目来重现它.

This is a 'bug' that I discovered while working on a real app, but I created a blank project to reproduce it.

我有以下布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:id="@+id/root"
    android:orientation="vertical"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <com.example.test.testapp.MyEditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

MyEditText 类如下所示:

public class MyEditText extends EditText {

    public MyEditText(Context context){
        super(context);
    }

    public MyEditText(Context context, AttributeSet attrs){
        super(context, attrs);
    }


    public MyEditText(Context context, AttributeSet attrs, int defStyle){
        super(context, attrs, defStyle);
    }


}

我的 styles.xml 文件是空的,除了主题

My styles.xml file is empty except for the Theme

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

</resources>

我希望 MyEditText 看起来像普通的 EditText 并且它在 Android 5.0 上是这样,但在 Android 2.3.7、Android 4.1.3 或 Android 4.4 上却不是.4.

I would expect the MyEditText to look like the normal EditText and it does on Android 5.0, but not on Android 2.3.7, Android 4.1.3 or Android 4.4.4.

在那些 Android 版本上,EditText 的颜色不同,正常的在聚焦时有青色下划线,自定义的有黑色下划线:

On those Android versions the EditTexts differ in color, the normal one has a cyan underline when focused, the custom one has a black underline:

为什么会发生这种情况,我该如何预防?

Why is this happening and how can I prevent it?

编辑/更新:

Google 似乎已经解决了这个问题 在支持库中引入 AppCompatEditText 类等.

Google seems to have adressed this in the support library by introducing, amongst others, an AppCompatEditText class.

推荐答案

为什么会这样

因为您使用的是 AppCompat.引用 关于该主题的博文:

Because you are using AppCompat. Quoting the blog post on the subject:

问:为什么我的 EditText(或上面列出的其他小部件)在我的棒棒糖之前的设备上没有正确着色?

Q: Why is my EditText (or other widget listed above) not being tinted correctly on my pre-Lollipop device?

答:AppCompat 中的小部件着色通过拦截任何布局膨胀并在其位置插入一个特殊的着色感知版本的小部件来工作.对于大多数人来说,这可以正常工作,但我可以想到一些不起作用的场景,包括:

A: The widget tinting in AppCompat works by intercepting any layout inflation and inserting a special tint-aware version of the widget in its place. For most people this will work fine, but I can think of a few scenarios where this won’t work, including:

  • 您拥有自己的自定义小部件版本(即您已扩展 EditText)
  • 您正在创建没有 LayoutInflater 的 EditText(即,调用 new EditText()).

因此,该行为是预期的.AppCompat 向后移植提供了一些轻量级的着色向后移植,但它不能处理所有情况.

So, the behavior is expected. The AppCompat backport provides some lightweight backporting of tinting, but it's not going to handle all cases.

如何预防?

即开即用:

  • 不要创建 EditText 的子类,或者

在 AppCompat 上运行时,查找色调并弄清楚如何自己应用它,或许可以通过检查 AppCompat 源代码(假设它可用——我没有寻找它),或者

When running on AppCompat, look up the tint and figure out how to apply it yourself, perhaps by examining the AppCompat source code (assuming it's available -- I haven't looked for it), or

不要使用 AppCompat,并在 Android 5.0+ 设备上使用 Theme.Material 查看着色是否按预期工作

Do not use AppCompat, and see if tinting works as expected using Theme.Material on Android 5.0+ devices

未来的 AppCompat 可能会为子类提供某种系统来参与着色过程.除了这些之外,可能还有其他解决方案——这些都是我想到的.

It's possible that a future AppCompat could provide some sort of system for subclasses to participate in the tinting process. And there may be other solutions than these -- these are what come to mind.

这篇关于EditText 的子类看起来与 Android 4 上的普通 EditText 不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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