Android@id和@+id区别

来源:互联网 发布:在淘宝买黄金靠谱吗 编辑:程序博客网 时间:2024/06/05 09:44

Android中的组件需要用一个int类型的值来表示,这个值就是组件标签中的id属性值。


id属性只能接受资源类型的值,也就是必须以@开头的值,例如,@id/abc、@+id/xyz等。


如果在@后面使用“+”,表示当修改完某个布局文件并保存后,系统会自动在R.java文件中生成相应的int类型变量。变量名就是“/”后面的值,例如,@+id/xyz会在R.java文件中生成int xyz = value,其中value是一个十六进制的数。如果xyz在R.java中已经存在同名的变量,就不再生成新的变量,而该组件会使用这个已存在的变量的值。


既然组件的id属性是一个资源id就可以,那么自然可以设置任何已经存在的资源id值,例如,@drawable/icon、@string/ok、@+string/you等。也可以设置android系统中已存在的资源id,例如@id/android:list,那么,这个android是什么意思呢,实际上,这个android就是系统的R类(在R.java文件中)所在的package。


我们可以在Java代码编辑区输入android.R.id.,就会列出相应的资源id,例如,也可以设置id属性值为@id/android:message。


还有另外一种方法查看系统中定义的id,进入sdk/platforms/android-16/data/res/values目录,ids.xml文件;

[html] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--  
  3. **  
  4. ** Copyright 2007, The Android Open Source Project  
  5. **  
  6. ** Licensed under the Apache License, Version 2.0 (the "License");  
  7. ** you may not use this file except in compliance with the License.  
  8. ** You may obtain a copy of the License at  
  9. **  
  10. **     http://www.apache.org/licenses/LICENSE-2.0  
  11. **  
  12. ** Unless required by applicable law or agreed to in writing, software  
  13. ** distributed under the License is distributed on an "AS IS" BASIS,  
  14. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
  15. ** See the License for the specific language governing permissions and  
  16. ** limitations under the License.  
  17. */  
  18. -->  
  19. <resources>  
  20.   <item type="id" name="background" />  
  21.   <item type="id" name="checkbox" />  
  22.   <item type="id" name="content" />  
  23.   <item type="id" name="empty" />  
  24.   <item type="id" name="hint" />  
  25.   <item type="id" name="icon" />  
  26.   <item type="id" name="icon1" />  
  27.   <item type="id" name="icon2" />  
  28.   <item type="id" name="input" />  
  29.   <item type="id" name="left_icon" />  
  30.   <item type="id" name="list" />  
  31.   <item type="id" name="menu" />  
  32.   <item type="id" name="message" />  
  33.   <item type="id" name="primary" />  
  34.   <item type="id" name="progress" />  
  35.   <item type="id" name="right_icon" />  
  36.   <item type="id" name="summary" />  
  37.   <item type="id" name="selectedIcon" />  
  38.   <item type="id" name="tabcontent" />  
  39.   <item type="id" name="tabhost" />  
  40.   <item type="id" name="tabs" />  
  41.   <item type="id" name="text1" />  
  42.   <item type="id" name="text2" />  
  43.   <item type="id" name="title" />  
  44.   <item type="id" name="title_container" />  
  45.   <item type="id" name="toggle" />  
  46.   <item type="id" name="secondaryProgress" />  
  47.   <item type="id" name="lock_screen" />  
  48.   <item type="id" name="edit" />  
  49.   <item type="id" name="widget_frame" />  
  50.   <item type="id" name="button1" />  
  51.   <item type="id" name="button2" />  
  52.   <item type="id" name="button3" />  
  53.   <item type="id" name="extractArea" />  
  54.   <item type="id" name="candidatesArea" />  
  55.   <item type="id" name="inputArea" />  
  56.   <item type="id" name="inputExtractEditText" />  
  57.   <item type="id" name="selectAll" />  
  58.   <item type="id" name="cut" />  
  59.   <item type="id" name="copy" />  
  60.   <item type="id" name="paste" />  
  61.   <item type="id" name="copyUrl" />  
  62.   <item type="id" name="selectTextMode" />  
  63.   <item type="id" name="switchInputMethod" />  
  64.   <item type="id" name="keyboardView" />  
  65.   <item type="id" name="closeButton" />  
  66.   <item type="id" name="startSelectingText" />  
  67.   <item type="id" name="stopSelectingText" />  
  68.   <item type="id" name="addToDictionary" />  
  69.   <item type="id" name="accountPreferences" />  
  70.   <item type="id" name="smallIcon" />  
  71.   <item type="id" name="custom" />  
  72.   <item type="id" name="home" />  
  73.   <item type="id" name="fillInIntent" />  
  74.   <item type="id" name="rowTypeId" />  
  75.   <item type="id" name="up" />  
  76.   <item type="id" name="action_menu_divider" />  
  77.   <item type="id" name="icon_menu_presenter" />  
  78.   <item type="id" name="list_menu_presenter" />  
  79.   <item type="id" name="action_menu_presenter" />  
  80.   <item type="id" name="overflow_menu_presenter" />  
  81.   <item type="id" name="popup_submenu_presenter" />  
  82. </resources>  
若在ids.xml中定义了ID,则在layout中可如下定义@id/price_edit,否则@+id/price_edit;

简单来讲:

@+id 新增一个资源id
@id和android:id,引用现有的资源id

转载自:点击打开链接

0 0
原创粉丝点击