Android编程中@id和@+id的区别分析
本文分析了Android编程中@id和@+id的区别。分享给大家供大家参考,具体如下:
Android中的组件需要用一个int类型的值来表示,这个值就是组件标签中的id属性值。
id属性只能接受资源类型的值,也就是必须以@开头的值,例如,@id/abc、@+id/xyz等。
如果在@后面使用“+”,表示当修改完某个布局文件并保存后,系统会自动在R.java文件中生成相应的int类型变量。变量名就是“/”后面的值,例如,@+id/xyz会在R.java文件中生成intxyz=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文件;
<?xmlversion="1.0"encoding="utf-8"?> <!-- ** **Copyright2007,TheAndroidOpenSourceProject ** **LicensedundertheApacheLicense,Version2.0(the"License"); **youmaynotusethisfileexceptincompliancewiththeLicense. **YoumayobtainacopyoftheLicenseat ** **http://www.apache.org/licenses/LICENSE-2.0 ** **Unlessrequiredbyapplicablelaworagreedtoinwriting,software **distributedundertheLicenseisdistributedonan"ASIS"BASIS, **WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied. **SeetheLicenseforthespecificlanguagegoverningpermissionsand **limitationsundertheLicense. */ --> <resources> <itemtype="id"name="background"/> <itemtype="id"name="checkbox"/> <itemtype="id"name="content"/> <itemtype="id"name="empty"/> <itemtype="id"name="hint"/> <itemtype="id"name="icon"/> <itemtype="id"name="icon1"/> <itemtype="id"name="icon2"/> <itemtype="id"name="input"/> <itemtype="id"name="left_icon"/> <itemtype="id"name="list"/> <itemtype="id"name="menu"/> <itemtype="id"name="message"/> <itemtype="id"name="primary"/> <itemtype="id"name="progress"/> <itemtype="id"name="right_icon"/> <itemtype="id"name="summary"/> <itemtype="id"name="selectedIcon"/> <itemtype="id"name="tabcontent"/> <itemtype="id"name="tabhost"/> <itemtype="id"name="tabs"/> <itemtype="id"name="text1"/> <itemtype="id"name="text2"/> <itemtype="id"name="title"/> <itemtype="id"name="title_container"/> <itemtype="id"name="toggle"/> <itemtype="id"name="secondaryProgress"/> <itemtype="id"name="lock_screen"/> <itemtype="id"name="edit"/> <itemtype="id"name="widget_frame"/> <itemtype="id"name="button1"/> <itemtype="id"name="button2"/> <itemtype="id"name="button3"/> <itemtype="id"name="extractArea"/> <itemtype="id"name="candidatesArea"/> <itemtype="id"name="inputArea"/> <itemtype="id"name="inputExtractEditText"/> <itemtype="id"name="selectAll"/> <itemtype="id"name="cut"/> <itemtype="id"name="copy"/> <itemtype="id"name="paste"/> <itemtype="id"name="copyUrl"/> <itemtype="id"name="selectTextMode"/> <itemtype="id"name="switchInputMethod"/> <itemtype="id"name="keyboardView"/> <itemtype="id"name="closeButton"/> <itemtype="id"name="startSelectingText"/> <itemtype="id"name="stopSelectingText"/> <itemtype="id"name="addToDictionary"/> <itemtype="id"name="accountPreferences"/> <itemtype="id"name="smallIcon"/> <itemtype="id"name="custom"/> <itemtype="id"name="home"/> <itemtype="id"name="fillInIntent"/> <itemtype="id"name="rowTypeId"/> <itemtype="id"name="up"/> <itemtype="id"name="action_menu_divider"/> <itemtype="id"name="icon_menu_presenter"/> <itemtype="id"name="list_menu_presenter"/> <itemtype="id"name="action_menu_presenter"/> <itemtype="id"name="overflow_menu_presenter"/> <itemtype="id"name="popup_submenu_presenter"/> </resources>
若在ids.xml中定义了ID,则在layout中可如下定义@id/price_edit,否则@+id/price_edit;
简单来讲:
@+id新增一个资源id
@id和android:id,引用现有的资源id
希望本文所述对大家Android程序设计有所帮助。