2012-03-02 52 views
13
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ListView android:id="@android:id/list" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" android:layout_weight="1" /> 
    <Spinner android:id="@+id/section_spinner" 
     android:layout_width="fill_parent" android:layout_height="wrap_content" 
     android:layout_margin="5dp" /> 
</LinearLayout> 

@android:id和@id在這種情況下有什麼區別?@id和@android之間的區別:id

+3

id你自己的id,android id默認android有一些id。 – 2012-03-02 08:04:36

回答

20

您需要時您要定義自己的ID到一個視圖,在這種情況下是section_spinner使用@+id。並且@android:id用於需要在框架中將視圖的Id設置爲android的預定義ID。例如當在ListHost中使用ListActivity,TabWidget/FrameLayout等。

8

ID自己的ID,ID的Android存在默認的ID在Android平臺RES /價值/ ids.xml有一些ID作爲下面的API 10

res/values/ids.xml 

這些都是在Android

默認IDS
<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="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" /> 
+1

你從哪裏找到這個清單?我很好奇哪些ID是由Android「預定義」的。而且爲什麼要預先定義呢? – steve 2013-04-23 22:22:08

+3

@steve你可以在你的sdk文件夾下找到所有的android api levels /android-sdk/platforms/android-17/data/res/values/ids.xml – 2013-04-24 11:40:22

3
@id 

您指的是您自己在您的項目中定義的id

@android:id 

您指的是由Android框架中定義的ID

24

字符串開頭的符號(@)表示XML解析器應解析並展開ID字符串的其餘部分並將其標識爲一個ID資源。加號(+)表示這是一個新的資源名稱,必須創建並添加到我們的資源中(在R.java文件中)。 Android框架提供了許多其他ID資源。引用Android資源ID時,不需要加號,但必須添加android包名稱空間。

@+id/section_spinner表示您正在應用程序的命名空間中創建一個名爲section_spinner的id。 你可以使用@ id/section_spinner來引用它。

@android:id/list表示您指的是在android命名空間中定義的列表。

'+'表示如果它不存在,則創建該符號。您在引用android:symbols時不需要它(並且不應該使用它),因爲這些已經由平臺爲您定義,並且無論如何您都無法在該名稱空間中創建自己的。

+0

tx ajay我正在尋找差異。在@id和@ +之間編號爲 – alex 2012-03-24 08:51:02

+0

這與下面的@padma的答案相結合,正是我所期待的。謝謝你們兩位! – steve 2013-04-23 22:21:37

相關問題