我怎样才能从AppWidget Android中得到正确的DisplayMetrics? [英] How can I get the correct DisplayMetrics from an AppWidget in Android?

查看:263
本文介绍了我怎样才能从AppWidget Android中得到正确的DisplayMetrics?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要确定在Android AppWidget屏幕密度运行。我给自己定了一个华电国际模拟器设备(AVD)。如果建立经常性的可执行项目,并插入该code到onCreate方法:

I need to determine the screen density at runtime in an Android AppWidget. I've set up an HDPI emulator device (avd). If set up a regular executable project, and insert this code into the onCreate method:

DisplayMetrics dm = getResources().getDisplayMetrics();
Log.d("MyTag", "screen density " + dm.densityDpi);

这个输出屏幕像素密度240作为预期。

This outputs "screen density 240" as expected.

不过,如果我成立了一个AppWidget项目,并插入此code进入的OnUpdate方法:

However, if I set up an AppWidget project, and insert this code into the onUpdate method:

DisplayMetrics dm = context.getResources().getDisplayMetrics();
Log.d("MyTag", "screen density " + dm.densityDpi);

这个输出屏幕像素密度160。我注意到,挂钩调试器,该资源的mDefaultDisplay这里的会员对象为空的AppWidget情况。

This outputs "screen density 160". I noticed, hooking up the debugger, that the mDefaultDisplay member of the Resources object here is null in the AppWidget case.

同样,如果我用从context.getResources()在AppWidget获得的资源对象获取的资源在运行时,它会返回基于屏幕像素密度的错误的资源。举例来说,我有一个60x60px绘制的MDPI,和一个80×80可绘制的华电国际。如果我使用context.getResources()这个可绘制对象。getDrawable(...),它返回60x60的版本。

Similarly, if I get a resource at runtime using the Resources object obtained from context.getResources() in the AppWidget, it returns the wrong resource based on screen density. For instance, I have a 60x60px drawable for mdpi, and an 80x80 drawable for hdpi. If I get this Drawable object using context.getResources().getDrawable(...), it returns the 60x60 version.

有没有什么办法来正确地处理资源在运行时从AppWidget?

Is there any way to correctly deal with resources at runtime from the context of an AppWidget?

谢谢!

推荐答案

这感觉就像一个错误。如果你能创建一个演示错误示例项目,在 Android的发布问题问题跟踪。如果你想起来了,在这里添加评论指向的问题。

This feels like a bug. If you can create a sample project that demonstrates the error, post an issue on the Android issue tracker. If you think of it, add a comment here pointing to the issue.

这篇关于我怎样才能从AppWidget Android中得到正确的DisplayMetrics?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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