儿童类的EditText的看起来比普通的Andr​​oid不同的EditText 4 [英] Child class of EditText looks different than normal EditText on Android 4

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

问题描述

这是我才发现,当工作在一个真正的应用程序中的错误,但我创建了一个空白的项目,以重现。

我有以下布局:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s机器人:layout_width =match_parent
    机器人:ID =@ + ID /根
    机器人:方向=垂直
    机器人:layout_height =match_parent机器人:以下属性来=@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    机器人:paddingBottom会=@扪/ activity_vertical_margin工具:上下文=MainActivity。>

    <的EditText
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT/>

    < com.example.test.testapp.MyEditText
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT/>


< / LinearLayout中>
 

MyEditText 是这样的:

 公共类MyEditText扩展的EditText {

    公共MyEditText(上下文的背景下){
        超(上下文);
    }

    公共MyEditText(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
    }


    公共MyEditText(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
    }


}
 

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

 <资源>

    <! - 基本应用主题。 - >
    <样式名称=AppTheme父=Theme.AppCompat.Light.DarkActionBar>
        <! - 自定义你的主题在这里。 - >
    < /风格>

< /资源>
 

我期望 MyEditText 看起来像正常的的EditText 和它在Android 5.0,但不是在安卓2.3.7,安卓4.1.3或Android 4.4.4。

在这些安卓版本的的EditText 取值不同的颜色,正常的人有青色下划线集中的时候,人们有一个黑色下划线自定义:

这是怎么回事,并哪能prevent呢?

编辑/ UPDATE:

谷歌似乎也不客气这个通过介绍,除其他外,一个AppCompatEditText类支持库。

解决方案
  

这是怎么回事

由于您使用的是AppCompat。引用的博客文章主题

  

问:为什么是我的EditText(或其他上面列出的部件)没有被正确着色我的pre-棒棒糖设备上

     

答:AppCompat窗口小部件着色通过拦截任何布局通胀和插入部件的特殊色彩感知的版本,在它的位置。对于大多数人来说,这将正常工作,但我能想到的几个场景,这是行不通的,其中包括:

     
      
  • 您有widget的自己的自定义的版本(即你扩展的EditText)
  •   
  • 您所创建的EditText上没有LayoutInflater(即调用新的EditText())。
  •   

那么,是正常现象。该AppCompat反向移植提供了着色的一些轻量级向后移植,但它不会处理所有案件。

  

我怎么能prevent呢?

即兴,或者:

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

  • 在AppCompat运行时,查找色调,并找出如何自己申请,也许通过检查AppCompat源$ C ​​$ C(假设它的可用 - 我没有看过它),或

  • 不要使用AppCompat,看看是否着色使用作品如预期 Theme.Material 在Android 5.0+设备

这有可能是未来的AppCompat可以提供某种系统的子类参与染色工序。并且还可能有其他的解决方案比这些 - 这些都是可以想到

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

I have the following layout:

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

The class MyEditText looks like this:

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);
    }


}

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>

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.

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?

EDIT / UPDATE:

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

解决方案

Why is this happening

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

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

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:

  • You have your own custom version of the widget (i.e. you’ve extended EditText)
  • You are creating the EditText without a LayoutInflater (i.e., calling new EditText()).

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

how can I prevent it?

Off the cuff, either:

  • Do not create a subclass of EditText, or

  • 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

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

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的看起来比普通的Andr​​oid不同的EditText 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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