恒州并没有在开发preVIEW安卓大号匹配 [英] Constant states do not match in developer preview android L

查看:117
本文介绍了恒州并没有在开发preVIEW安卓大号匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一些事情的if语句,这个工作在每个版本的Andr​​oid(16因为getDrawable或更高版本),除了Android的L(在最新的测试)。在code是如下:

I am trying to do something in an if-statement, this works in every version of android (16 or higher because of the getDrawable) except Android L (tested on latest). The code is the following:

if (item.getIcon().getConstantState().equals(getResources().getDrawable(R.drawable.add_to_fav_normal).getConstantState())

任何帮助/提示或解释是AP preciated!

Any help/hints or explanation would be appreciated!

推荐答案

使用 item.getContext()。getDrawable(INT)或等值<一href="http://developer.android.com/reference/android/support/v4/content/ContextCompat.html"><$c$c>ContextCompat方法。

Use item.getContext().getDrawable(int) or the equivalent ContextCompat method.

开始在API 21中,所有的框架部件的负载可绘制使用<一个href="http://developer.android.com/reference/android/content/Context.html#getDrawable(int)"><$c$c>Context.getDrawable()其中通胀过程中应用的上下文的当前主题。这基本上只是调用 getResources()。getDrawable(...,getTheme())在内部,所以你也可以使用 context.getResources()。 getDrawable(...,context.getTheme())

Starting in API 21, all framework widgets that load drawables use Context.getDrawable() which applies the context's current theme during inflation. This basically just calls getResources().getDrawable(..., getTheme()) internally, so you could also use context.getResources().getDrawable(..., context.getTheme()).

     if (item.getIcon().getConstantState().equals(item.getContext()
                .getDrawable(R.drawable.add_to_fav_normal).getConstantState())

在一般情况下,虽然,你不应该依赖于此项检查。大约有什么恒定的状态,你会收到一个特殊的绘制没有API的保证。

In general, though, you shouldn't rely on this check. There are no API guarantees around what constant state you'll receive from a particular drawable.

这篇关于恒州并没有在开发preVIEW安卓大号匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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