简体   繁体   English

如何为Android指定null作为XML属性?

[英]How can I specify a null as an XML attribute for Android?

What do I need to put in for an attribute if I want to put a null there? 如果我想在那里放置null,我需要为属性添加什么?

For example, this piece of code: 例如,这段代码:

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

    <item android:drawable="@drawable/ef_gradient_blue" />
    <item android:drawable="@null" />

</transition>

But this won't work, as my app crashes when it tries to load this drawable. 但这不起作用,因为我的应用程序在尝试加载此drawable时崩溃了。 I need it to be null, so it can make a transition from fully opaque to completely see through. 我需要它为null,因此它可以从完全不透明转换为完全透视。

In my Java, I load it like this: 在我的Java中,我加载它像这样:

mTransitionBlue = (TransitionDrawable)mProjectResources.getDrawable(R.drawable.transition_blue);

But I get a runtime exception from this, saying it can't find the transition_blue.xml, which is the XML specified before. 但是我得到了一个运行时异常,说它找不到transition_blue.xml,这是之前指定的XML。 So how do I achieve this? 那我该怎么做呢?

You should use a transparent drawable there (android lib has it predefined): 你应该在那里使用透明drawable(android lib预定义了它):

<transition xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/ef_gradient_blue" />
    <item android:drawable="@android:color/transparent" />
</transition>

the @android:color/transparent 's value is #00000000 . @android:color/transparent的值是#00000000 It's important to have the first two digits 0, since they define the alpha value of the color (the rest is red, green and blue). 将前两位数字设为0非常重要,因为它们定义了颜色的alpha值(其余为红色,绿色和蓝色)。
So you can use 所以你可以使用

<item android:drawable="#00000000" />

as well, or redefine it among your colors. 同样,或者在你的颜色中重新定义它。

For reference you could take a look at the Android Developers 2D Graphics and Transition Drawables articles. 作为参考,您可以查看Android Developers 2D GraphicsTransition Drawables文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM