Android的资源ID [英] Android Resource IDs

查看:193
本文介绍了Android的资源ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是从一个自定义的XML视图类型检索自定义资源的ID。有人问我指定检索默认int值,并想知道什么是ID的范围是多少?难道他们总是积极的还是包括零??

I'm retrieving custom Resource IDs from a custom xml view type. I'm asked to specify a default int value for the retrieval and was wondering what is the range of IDs? Are they always positive or do they include zero??

即是-1一个有效的空引用和/或0一个有效的空的提法?

i.e is -1 a valid "null" reference AND/OR is 0 a valid "null" reference?

感谢

修改

自定义XML资源/属性文件

Custom XML resource/attribute file

<resources>
    <declare-styleable name="ToggleImageButton">
        <attr name="onImage" format="integer" />
        <attr name="offImage" format="integer" />
    </declare-styleable>
</resources>

定义在我的构造我的自定义UI

Defined in my constructor for my custom ui

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ToggleImageButton);

int offResource = a.getInt(R.styleable.ToggleImageButton_offImage, -1);

基本上-1在第2行的结尾是该数据类型的默认参数。它可以或可以不被在XML视图显影时初始化,这允许指定默认行为这种方式

Basically the -1 at the end of the 2nd line is the default parameter for this data type. It may or may not be initialized in the XML view when developing and this allows default behavior to be specified this way.

推荐答案

据<一href="http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29">the文档, Resources.getIdentifier()

则返回0,如果没有这样的资源   被找到。 (0不是有效的资源ID。)

Returns 0 if no such resource was found. (0 is not a valid resource ID.)

这样你就可以使用0。

这篇关于Android的资源ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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