Most of this article was excerpted from http://hukai.me/android-training-course-in-chinese/basics/firstapp/building-ui.html
Android:id
This is the unique identifier that defines the view. You can refer to an object by that identifier in your program code, such as an operation to read and modify the object (which will be used in the next lesson).
You must use the @ symbol when you want to reference a resource object from XML. Immediately after the @ is the type of the resource (here id
), and then the name of the resource (used here edit_message
).
The + sign is only needed when you first define a resource ID. This is to tell the SDK that this resource ID needs to be created. After the application is compiled, the SDK can use the ID value directly, edit_message is gen/R.java
creating a new identifier in the project file, which is associated with the edittext. Once the resource ID is created, the other resource will no longer need the + number if it references the ID. Here is the only attribute that needs a + sign.
Android:layout_width and Android:layout_height
For width and height it is not recommended to specify a specific size, the wrap_content
view will occupy only the space of the content size after using the specified. If you use it, the edittext will be filled with the match_parent
entire screen, because it will fit the size of the parent layout. For more information, please refer to the Layout wizard.
Android:hint
When the text box is empty, the string is displayed by default. The resource referenced for the value of the string @string/edit_message
should be defined in a separate file instead of using the string directly. Because the value used is a resource that exists, you do not need to use the + sign. However, since you have not defined the value of the string, a @string/edit_message
compilation error will occur at the time of the addition. Below you can define string resource values to remove this error.
Note: The string resource uses the same name as the ID (edit_message). However, references to resources are type-sensitive (such as ID and string), so using the same name does not cause a conflict.
(Android Studio) Add a text box