布局中的所有视图都需要android:id属性吗? [英] Is the android:id attribute required for all views in my layout?

查看:50
本文介绍了布局中的所有视图都需要android:id属性吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

android:id 布局中所有视图都需要属性吗?最佳做法是什么?

Is the android:id attribute required for all views in my layout? What is the best practice?

我希望在需要的罕见实例中指定ID(例如WPF中的 x:Name ),但似乎Android和Eclipse都希望在所有内容上都具有ID.

I would prefer to specify IDs in the rare instances that I need it (like x:Name in WPF), but it seems like Android and Eclipse like to have it on everything.

推荐答案

为此,我相信Android编译器会自动为xml布局文件中的每个元素(可能是错误的)生成一个ID属性.但是拥有ID属性的目的是使程序员(您)可以交互或引用任何给定的元素.

To put some more context in this, I believe the Android compiler automatically generates an ID attribute for every element (I could be wrong) in your xml layout file. But the purpose of having an ID attribute is so that you, the programmer, can either interact or refer to any given element.

例如,您可以将布局(或表/行或...)设置为可见或不可见,或者可以更改屏幕上某物的位置...

For instance, you can set a layout (or table/row or...) to visible or invisible, or you can change the location of something on the screen...

我个人不会在所有内容上添加id,特别是当我的布局跨越多个文件和页面时.

Personally I don't bother putting an id on everything especially if my layouts spans several files and pages.

这篇关于布局中的所有视图都需要android:id属性吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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