恒州并没有在开发preVIEW安卓大号匹配并没有、大号、preVIEW

2023-09-07 18:12:32 作者:麻 辣 小 魚

我试图做一些事情的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)或等值ContextCompat方法。

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

开始在API 21中,所有的框架部件的负载可绘制使用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.