开发者

Is the android:id attribute required for all views in my layout?

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

I would prefer to specify IDs in the rare instances that I need it (like x:Name in WPF), but it seems like Andr开发者_开发百科oid and Eclipse like to have it on everything.


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

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


Views may have an integer id associated, but it's completely optional. You are going to need it if you want to be able to find specific views inside the view tree.

Moreover, ids need not to be unique across the entire view tree, but just in the part you are searching.


No. It's only important if you are putting the view in a RelativeLayout(and will use it to position other views) or want to get it via findViewById(), but it is not required.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜