如何为Android指定XML属性为null? [英] How can I specify a null as an XML attribute for Android?

查看:46
本文介绍了如何为Android指定XML属性为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要在其中添加空值,我需要在属性中输入什么?

What do I need to put in for an attribute if I want to put a null there?

例如,这段代码:

<?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>

但是这不起作用,因为我的应用在尝试加载此drawable时崩溃了.我需要将其设置为null,以便它可以从完全不透明过渡到完全透明.

But this won't work, as my app crashes when it tries to load this drawable. I need it to be null, so it can make a transition from fully opaque to completely see through.

在我的Java中,我像这样加载它:

In my Java, I load it like this:

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

但是我从中得到一个运行时异常,说它找不到以前指定的XML transition_blue.xml.那么我该如何实现呢?

But I get a runtime exception from this, saying it can't find the transition_blue.xml, which is the XML specified before. So how do I achieve this?

推荐答案

您应在此处使用透明的可绘制对象(android lib已对其进行了预定义):

You should use a transparent drawable there (android lib has it predefined):

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

@android:color/transparent 的值为#00000000 .前两位为0很重要,因为它们定义了颜色的Alpha值(其余分别为红色,绿色和蓝色).
因此您可以使用

the @android:color/transparent's value is #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).
So you can use

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

,或者在您的颜色中重新定义它.

as well, or redefine it among your colors.

作为参考,您可以看一下Android开发人员 2D图形 过渡可绘制对象 文章.

For reference you could take a look at the Android Developers 2D Graphics and Transition Drawables articles.

这篇关于如何为Android指定XML属性为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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