Android是什么范围:ID? [英] What is the scope of android:id?

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

问题描述

可能重复:结果
  R.id值范围

可能有人请澄清资源的ID在Android的范围有多大?最近我正在其中载有所谓的头RelativeLayout的自定义标题栏。我在调用这个标题栏中的的onCreate()的每个正是如此我活动的处理程序:

Could someone please clarify the scope of resource id's in Android? Recently I was making a custom title bar which contained a RelativeLayout called "header". I was invoking this title bar in the onCreate() handlers of each of my Activities thusly:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_layout);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);  

我的一个活动有一个TextView在它与头标识布局和该活动的的的onCreate()的我做了。

TextView tv1 = (TextView)findViewById(R.id.header); 

。 。 。这与在运行时抛出ClassCastException炸毁了,显然是因为它认为我是想投的RelativeLayout到一个TextView。改变的RelativeLayout的名字在标题栏中固定它。

. . . which blew up at runtime with ClassCastException, apparently because it thought I was trying to cast a RelativeLayout to a TextView. Changing the name of the RelativeLayout in the title bar fixed it.

但我真的不明白这一点。是的Andr​​oid资源ID的全球?如果是这样,为什么不这样的碰撞将在编译时抓到?

But I don't really understand this. Are Android resource id's global? If so why wouldn't this collision be caught at build-time?

请注意,这个问题已经被问之前,SO:R.id值范围。 。 。凡OP说,据我可以告诉R.id的是全球性的,但我的问题是不是重复,因为无论我的问题是真的,一个解决。

Note that this question has been asked before on SO: Scope of R.id values . . . Where the OP said "as far as I can tell" R.id's are global but my question is not a duplicate because neither of my questions is really addressed in that one.

推荐答案

R. *值是全局的他们自己的类型。所以,你可以有一个 R.string R.id 具有相同的名称,但不能与两个ID的一样的名字。在你的情况,我觉得做一个干净的构建将能解决问题为好。例如,看看在R.java文件内容:

R.* values are global to their own type. So you can have a R.string and an R.id with the same name, but not two IDs with the same name. In your case, I feel that doing a build clean would have fixed your problem as well. For example, take a look at the R.java file contents:

    public static final class drawable {
    public static final int icon=0x7f020000;
    public static final int info=0x7f020001;
    public static final int menu=0x7f020002;
    public static final int splash1=0x7f020003;
}
public static final class id {
    public static final int ImageView02=0x7f070008;
    public static final int bestTimeTextView=0x7f070005;
    public static final int infoButton=0x7f070003;
    public static final int infoImageView=0x7f070000;
    public static final int levelSpinner=0x7f070002;
    public static final int menuImageView=0x7f070001;
    public static final int selectLevelTextView=0x7f070004;
    public static final int startButton=0x7f070006;
    public static final int timeTextView=0x7f070007;
}
public static final class layout {
    public static final int info=0x7f030000;
    public static final int menu=0x7f030001;
    public static final int splash=0x7f030002;
}
public static final class raw {
    public static final int down=0x7f040000;
    public static final int up=0x7f040001;
}
public static final class string {
    public static final int appname=0x7f060000;
    public static final int besttime=0x7f060002;
    public static final int help=0x7f060004;
    public static final int selectlevel=0x7f060001;
    public static final int start=0x7f060003;
}

在这里,我有两个绘制和布局部分,完美的应用程序函数使用的信息名称。不过,如果我尝试添加两种资源使用相同的ID,或者两个可绘制具有相同的名称,或者两个字符串具有相同的名称等,将无法正常工作。

Here I have the info name used in both drawable and layout parts, and the app functions perfectly. However, if I try adding two resources with the same ID, or two drawables with the same name, or two strings with the same name etc. it will not function properly.

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

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