如何从动态创建的Andr​​oid资源ID(在Java中)层列表/ LayerDrawable?动态、列表、资源、Andr

2023-09-07 08:54:03 作者:青春就该放肆的过

解决方案#2(动态),在这个问题/回答后:

The "solution #2 (dynamic)" in this question/answer post:

overlay在Android的两个图像来设置ImageView的

很接近我想要做的,这是动态创建图层列表(一个状态栏通知图标,我想建立-我的分层图标),但通知API中的图标分配需要一个资源ID(这是我想从一个服务调用)。

is very close to what I want to do, which is to dynamically create a layer-list (for a status bar notification icon, I want to build-up my icon in layers), but the icon assignment in the notification API requires a resource ID (which I want to call from a service).

我不明白,如何构建一个动态构建一层名单没有建立数百层列表的.xml文件(图标,我想是能够显示各种组合)。丹尼尔的解决方案#1奇妙的作品为静态的.xml文件,但我在寻找一个更优雅的,动态的解决方案。

I cannot figure-out how to build a dynamically build a layer-list without building hundreds of layer-list .xml files (for the various combinations of icons that I would like to be able to display). Daniel's "solution #1" works wonderfully for the static .xml files, but I'm looking for a more elegant, dynamic solution.

在上述职位中,code片断:

In the above post, the code snippet:

  Resources r = getResources();

  Drawable[] layers = new Drawable[2]; 

  layers[0] = r.getDrawable(R.drawable.t);

  layers[1] = r.getDrawable(R.drawable.tt);

  LayerDrawable layerDrawable = new LayerDrawable(layers);

似乎是我想要的,但我不知道或了解如何分配新layerDrawable我的通知图标(这需要资源ID)。

appears to be what I want, but I don't know or understand how to "assign" the new layerDrawable to my notification icon (which takes a resource ID).

感谢所有...计算器是一个极好的资源!

Thanks to all...stackoverflow is a wonderful resource!

推荐答案

有没有这样的事情,在运行时创建一个可绘制的ID。这些ID是指为int在R类领域,从XML文件中自动创建。

由于LayerDrawable构造函数只需要一个绘制对象的数组,可以提供从任何方法制成的可绘制。一个例子,是静态方法 Drawable.createFromStream(InputStream的是,字符串源名)

Since the LayerDrawable constructor requires just a Drawable array, you can provide those Drawables made from any method. An example, would be the static method Drawable.createFromStream(InputStream is, String srcName).

http://developer.android.com/reference/android/graphics/drawable/Drawable.html#createFromStream%28java.io.InputStream,%20java.lang.String%29