When do I need to use "android:id"?
I saw it used in textview and webview
android:id="@+id/textview"
android:id="@+id/webview"
but looks like there i开发者_运维百科s none for DigitalClock widget.
So, when do I have to use it? Just for text and web views?
No. That kind of identifiers are customizable by the programmer. It's used to reference the Views from the Java code. For instance:
TextView foo = (TextView) findViewById(R.id.textview);
Of course, you can use any name you want. For example:
android:id="@+id/whatever_you_want"
Will be referenced this way:
TextView foo = (TextView) findViewById(R.id.whatever_you_want);
Another thing to bear in mind is that there are some IDs that are reserved. Any way, you will recognize user-created IDs because they contain a plus (+): @+id/whatever
You use it when you need it, either to:
- Reference it from Java (
R.id.whatever
) - Reference it from elsewhere in your layout (see
RelativeLayout
)
精彩评论