如何更改图层列表可绘制? [英] How to change a layer-list drawable?

查看:23
本文介绍了如何更改图层列表可绘制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试学习一些新东西但无法弄清楚这一点,任何帮助表示赞赏.鉴于这个来自 Google 文档的简单代码:

Trying to learn some new things and can't figure this one out, any help is appreciated. Given this simple code which is right from Google's documentation:

layers.xml:

layers.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/first_image">
      <bitmap android:src="@drawable/android_red"
        android:gravity="center" />
    </item>
    <item android:id="@+id/second_image" android:top="10dp" android:left="10dp">
      <bitmap android:src="@drawable/android_green"
        android:gravity="center" />
    </item>
    <item android:id="@+id/third_image" android:top="20dp" android:left="20dp">
      <bitmap android:src="@drawable/android_blue"
        android:gravity="center" />
    </item>
</layer-list>

和 main.xml:

And main.xml:

<ImageView
    android:id="@+myImageView"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/layers" />

问题:如何以编程方式引用图层列表中的一个可绘制对象,以便将其更改为不同的可绘制对象?

Question: How do I programmatically reference one of the drawables in the layer-list so I can change it to a different drawable?

谢谢.

所以现在我有了我想要在变量 myImage 中交换的新可绘制对象,对吗?现在如何将其放入图层列表?我假设我使用了 findDrawableByLayerIdsetDrawableByLayerId,但究竟如何?或者我可能做错了!帮助!

So now I have my new drawable that I want to swap in in variable myImage correct? Now how do I get that into the layer-list? I assume I use findDrawableByLayerId and setDrawableByLayerId, but how exactly? Or I could be doing it completely wrong! Help!

我想做什么?更改显示的可绘制对象.例如,假设我有另一个名为android_purple"的可绘制对象,我将如何将其切换为上面图层列表示例中的android_green"可绘制对象?

What am I trying to do? Change the drawable that's displayed. For example suppose I had another drawable called "android_purple", how would I switch it in for the "android_green" drawable in the layer-list example above?

以我有限的知识,这是我目前的代码(它不起作用,我收到错误,setDrawableByLayerId 未定义):

With my limited knowledge here is my code so far (which does not work, I get error that setDrawableByLayerId is undefined):

Resources res = getApplicationContext().getResources();
BitmapDrawable newImage = (BitmapDrawable) res.getDrawable(R.drawable.android_purple); //Get replacement image, can't use LayerDrawable or get error.   
boolean layer_drawable_changed = (setDrawableByLayerId((R.id.second_image), newImage)); //Set new drawable? Error on this line.

推荐答案

问题:如何以编程方式引用图层列表中的一个可绘制对象,以便将其更改为不同的可绘制对象?

Question: How do I programmatically reference one of the drawables in the layer-list so I can change it to a different drawable?

这是我要尝试的:

第 1 步:将 android:id 属性放在 元素上,如图 在文档中

Step #1: Put android:id attributes on the <item> elements as illustrated in the documentation

第 2 步:将 getDrawable() 结果转换为 LayerDrawable

Step #2: Cast the getDrawable() result to a LayerDrawable

第 3 步:如果您不想影响其他人使用 Drawable(可能是可选的)

Step #3: Call mutate() if you do not want to affect anyone else using the Drawable (may be optional)

第 4 步:调用 setDrawableByLayerId() 来设置/替换给定层 ID 的 Drawable

Step #4: Call setDrawableByLayerId() to set/replace a Drawable for a given layer ID

这篇关于如何更改图层列表可绘制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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