What is the difference between "@+id" and "@id" in the Android layout file?
- +id indicates that a control is assigned an ID (a new ID), such as:
<cn.codingblock.view.customer_view.MyView android:id="@+id/myview" ... />
- The ID indicates a reference to an existing ID, such as:
<cn.codingblock.view.customer_view.MyView android:id="@+id/myview" android:layout_below="@id/btn_handle_myview" .../>
However, it is important to note that in the layout file, the referenced ID should be on top of the reference location, or it will compile an error, as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools "Andro Id:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "Cn.codingblock.view.activity.MyViewActivity" > <cn.codingblock.view.customer_view. MyView android:id= "@+id/myview" android:layout_width= "match_parent" android:layout_height= "Match_pare NT "android:layout_below=" @id/btn_handle_myview "android:layout_margin=" 10DP "android:paddingleft=" 15 DP "android:paddingright=" 30DP "android:background=" #000 "/> <button android:id=" @+id/btn_han Dle_myview "android:layout_width=" match_parent "android:layout_height=" Wrap_content "android:layout_m Argin= "10DP" android:text= "Remove, display MyView"/></relativElayout>
Compile error message:
Error:(14, 31) No resource found that matches the given name (at ‘layout_below‘ with value ‘@id/btn_handle_myview‘).
Workaround:
- Method One: Change the location of the reference ID to +id, meaning that this ID is added to the project's R file first, as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools "Andro Id:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "Cn.codingblock.view.activity.MyViewActivity" > <cn.codingblock.view.customer_view. MyView android:id= "@+id/myview" android:layout_width= "match_parent" android:layout_height= "Match_pare NT "android:layout_below=" @+id/btn_handle_myview "android:layout_margin=" 10DP "android:paddingleft=" 1 5DP "android:paddingright=" 30DP "android:background=" #000 "/> <button android:id=" @+id/btn_ha Ndle_myview "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:layout_ Margin= "10DP" android:text= "Remove, display MyView"/></relatiVelayout>
In MyView's android:layout_below= "@+id/btn_handle_myview" This line of code has been added with +id Btn_handle_myview this ID, and then specify the ID for the button with + ID or ID can be, because the R file already has btn_handle_myview this ID, so when you specify an ID for the button directly with "@id/btn_handle_myview" even without the "+" number will not be an error. However, even if the "+" is not an error, and will not be added again.
- Method Two: Place the reference ID code below the +id, as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools "Andro Id:layout_width= "Match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= "Cn.codingblock.view.activity.MyViewActivity" > <button android:id= "@+id/btn_handle_myview" Android: Layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_margin= "10DP" Android : text= "Remove, display MyView"/> <cn.codingblock.view.customer_view. MyView android:id= "@+id/myview" android:layout_width= "match_parent" android:layout_height= "Match_pare NT "android:layout_below=" @id/btn_handle_myview "android:layout_margin=" 10DP "android:paddingleft=" 15 DP "android:paddingright=" 30DP "android:background=" #000 "/></relativElayout>
This is a small knowledge point, very simple, but it is very easy for us to ignore a place, so today to record.
Android Check Trap (view article)--what is the difference between "@+id" and "@id" in a layout file?