选择器、图层列表和形状/位图在同一个 xml 中 [英] Selector, Layer-list and shape/bitmap in the same xml

查看:22
本文介绍了选择器、图层列表和形状/位图在同一个 xml 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 drawable 文件夹内的 xml 中有此代码:

I have this code in an xml inside the drawable folder:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <size android:width="90dp" android:height="90dp" />

                <solid android:color="#9933CC" />
                </shape>
            </item>
            <item>
                <bitmap android:gravity="center" android:src="@drawable/main_achievements_synthesis" />
            </item>
        </layer-list>
    </item>
    <item>
        <layer-list>
            <item>
                <shape android:shape="rectangle">
                    <size android:width="90dp" android:height="90dp" />

                    <solid android:color="#AA66CC" />
                </shape>
            </item>
            <item>
                <bitmap android:gravity="center" android:src="@drawable/main_achievements_synthesis" />
            </item>
        </layer-list>
    </item>

</selector>

我用它来制作具有 2 个状态的图像(用作按钮).在模拟器和设备上一切正常.

I use it to have an image with 2 states (to use as a button). Everything works as expected on emulator and devices.

我知道我可以创建不同的 drawable xml 并进行引用以达到相同的结果.

I know that I can create different drawable xml and make a reference to achieve the same result.

我刚刚切换到 Android Studio,它向我显示了这条消息:此处不允许使用 XXX 元素.它警告我关于图层列表和其中的所有标签.然而,正如我所说,这段代码工作得很好.

I just switched to Android Studio and it shows me this mesage: Element XXX is not allowed here. It warns me about Layer-list and all the tags inside it. However, as I said, this code works just fine.

我应该将我的代码切换成单独的 XML(知道我只会使用它们一次)还是 Android Studio 的检查器代码中的错误"?

Should I switch my code into separated XMLs (knowing that I will only use them once) or is an "error" in the Android Studio's Inspector Code?

注意:我认为我的代码可以优化,但我还没有弄清楚如何.

NOTE: I think my code can be optimized, but I haven't figured out how yet.

推荐答案

这是 Android Studio 的 linting 工具中的错误.请在 https://code.google.com/p/android/issues/list 提交错误.

This is an error in Android Studio's linting tool. Please file a bug at https://code.google.com/p/android/issues/list.

此外,您可以使用以下方法进行优化:

Also, you can optimize a little by using:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <selector>
            <item android:state_pressed="true">
                <shape android:shape="rectangle">
                    <size android:width="90dp" android:height="90dp" />
                    <solid android:color="#9933CC" />
                </shape>
            </item>
            <item>
                <shape android:shape="rectangle">
                    <size android:width="90dp" android:height="90dp" />
                    <solid android:color="#AA66CC" />
                </shape>
            </item>
        </selector>
    </item>
    <item>
        <bitmap
            android:gravity="center"
            android:src="@drawable/main_achievements_synthesis" />
    </item>
</layer-list>

这篇关于选择器、图层列表和形状/位图在同一个 xml 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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