本文分析了Android編程中@id和@+id的區別。分享給大家供大家參考,具體如下:
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文件;
<?xml version="1.0" encoding="utf-8"?><!--**** Copyright 2007, The Android Open Source Project**** Licensed under the Apache License, Version 2.0 (the "License");** you may not use this file except in compliance with the License.** You may obtain a copy of the License at**** http://www.apache.org/licenses/LICENSE-2.0**** Unless required by applicable law or agreed to in writing, software** distributed under the License is distributed on an "AS IS" BASIS,** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.** See the License for the specific language governing permissions and** limitations under the License.*/--><resources> <item type="id" name="background" /> <item type="id" name="checkbox" /> <item type="id" name="content" /> <item type="id" name="empty" /> <item type="id" name="hint" /> <item type="id" name="icon" /> <item type="id" name="icon1" /> <item type="id" name="icon2" /> <item type="id" name="input" /> <item type="id" name="left_icon" /> <item type="id" name="list" /> <item type="id" name="menu" /> <item type="id" name="message" /> <item type="id" name="primary" /> <item type="id" name="progress" /> <item type="id" name="right_icon" /> <item type="id" name="summary" /> <item type="id" name="selectedIcon" /> <item type="id" name="tabcontent" /> <item type="id" name="tabhost" /> <item type="id" name="tabs" /> <item type="id" name="text1" /> <item type="id" name="text2" /> <item type="id" name="title" /> <item type="id" name="title_container" /> <item type="id" name="toggle" /> <item type="id" name="secondaryProgress" /> <item type="id" name="lock_screen" /> <item type="id" name="edit" /> <item type="id" name="widget_frame" /> <item type="id" name="button1" /> <item type="id" name="button2" /> <item type="id" name="button3" /> <item type="id" name="extractArea" /> <item type="id" name="candidatesArea" /> <item type="id" name="inputArea" /> <item type="id" name="inputExtractEditText" /> <item type="id" name="selectAll" /> <item type="id" name="cut" /> <item type="id" name="copy" /> <item type="id" name="paste" /> <item type="id" name="copyUrl" /> <item type="id" name="selectTextMode" /> <item type="id" name="switchInputMethod" /> <item type="id" name="keyboardView" /> <item type="id" name="closeButton" /> <item type="id" name="startSelectingText" /> <item type="id" name="stopSelectingText" /> <item type="id" name="addToDictionary" /> <item type="id" name="accountPreferences" /> <item type="id" name="smallIcon" /> <item type="id" name="custom" /> <item type="id" name="home" /> <item type="id" name="fillInIntent" /> <item type="id" name="rowTypeId" /> <item type="id" name="up" /> <item type="id" name="action_menu_divider" /> <item type="id" name="icon_menu_presenter" /> <item type="id" name="list_menu_presenter" /> <item type="id" name="action_menu_presenter" /> <item type="id" name="overflow_menu_presenter" /> <item type="id" name="popup_submenu_presenter" /></resources>
若在ids.xml中定義了ID,則在layout中可如下定義@id/price_edit,否則@+id/price_edit;
簡單來講:
@+id 新增一個資源id
@id和android:id,引用現有的資源id
希望本文所述對大家Android程序設計有所幫助。
新聞熱點
疑難解答
圖片精選