2011-04-20 61 views
18

可能重複:
What is different between @+id/android:list and @id/android:list ??Android中的@ id /和@ + id /有什麼區別?

是什麼@id/..and @+id/..之間的區別?我不是指差異 @android:id/..@id/..代碼示例之間

<Button 
android:id ="@id/add_button" 
/> 
<Button 
android:id ="@+id/remove_button" 
/> 

是什麼上述兩個id定義之間的區別?

+3

加號似乎表明ID已添加,並且缺席似乎表示該ID已存在。我只是在實踐中看到了這一點,但沒有注意到它的必要性......所以...我也想知道更多。 – 2011-04-20 13:51:53

+0

@George Bailey這是一個答案 – Selvin 2011-04-20 13:54:10

+1

@ + George;)我也想知道。它們基本上可以互換嗎?我一直只用@ + id。順便說一句,「Id已經存在」是你的意思是一個資源ID?這就說得通了。 – wired00 2011-04-20 13:54:15

回答

39

必須在XML文件中使用@+符號上的ID的第一次出現。第二次和以後的時間,你可以 - 也應該 - - 丟棄+標誌。這將有助於發現錯別字。例如,假設您有RelativeLayout。您在RelativeLayout中有TextView,其android:id@+id/label。稍後在佈局XML文件中,您希望從另一個參考TextView進行定位(例如,針對android:layout_below)。

如果您在android:layout_below="@+id/labbel"(注意錯字)中鍵入,在編譯時,這將被視爲確定。然而,在運行時,事情將無法正常工作,從小部件被錯誤定位到徹底崩潰,取決於Android版本。

如果您在android:layout_below="@id/labbel"輸入(注意錯字失蹤+標誌),那麼你會得到一個編譯錯誤。


UPDATE

由於我不太清楚第一次,很顯然,讓我們再試一次。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TextView android:id="@+id/label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="URL:" 
     android:layout_alignBaseline="@+id/entry" 
     android:layout_alignParentLeft="true"/> 
    <EditText 
     android:id="@id/entry" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/label" 
     android:layout_alignParentTop="true"/> 
    <Button 
     android:id="@+id/ok" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/entry" 
     android:layout_alignRight="@id/entry" 
     android:text="OK" /> 
    <Button 
     android:id="@+id/cancel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@id/ok" 
     android:layout_alignTop="@id/ok" 
     android:text="Cancel" /> 
</RelativeLayout> 

上面,你會看到一個RelativeLayout。您會注意到每個ID的第一次出現會得到+符號。每個ID的第二次和隨後出現都沒有得到+符號。

您可以在所有這些符號上使用+符號,但如果您輸入錯字,編譯器將無法解決問題。

+簽名有效狀態「分配一個新的ID」。沒有+符號狀態「使用先前分配的ID,或者如果沒有這樣的ID,則在編譯時失敗」。

+0

+1你的答案好多了。 – MByD 2011-04-20 14:03:12

+0

你的意思是,當我第一次創建按鈕時,我應該使用加號,並在我第一次編譯後,我應該刪除這個加號? 我有你的想法,當我在另一個地方像android調用按鈕:layout_toRightOf =「@ id/add_button」我不得不刪除加號 但在這裏我談論的標籤中的按鈕聲明 我的意思是如果我要去我的XML文件中創建一個按鈕是什麼bettwen區別 <按鈕 機器人:ID = 「@ ID/ADD_BUTTON」 /> <按鈕 機器人:ID = 「@ + ID/ADD_BUTTON」 /> 對不起,打擾你:) – hazem 2011-04-20 14:13:23

+1

他意味着當你創建按鈕併爲其分配一個ID時,使用'+'符號,但是當你引用這個按鈕時(例如,位置,例如'android:layout_below' )你不應該使用'+'符號。 – MByD 2011-04-20 14:18:04

3

Android Guide

對於ID值,你通常應該使用 這句法形式: 「@ + ID /名稱」。 加上符號+,表示這是 一個新的資源ID,並且aapt工具 將在 R.java類中創建一個新的資源整數,如果它不存在 。

所以+是用於分配一個新的ID,它也將工作時使用存在的ID,但它不是必要的。

+0

不幸的是,這是文檔中較弱的地方之一,並不是特別準確。 – CommonsWare 2011-04-20 13:59:30

3

第二個:

<Button android:id ="@+id/remove_button" /> 

定義了一個新的ID。當你想引用佈局元素時,你會使用第一個。例如,在一個相對佈局:

android:layout_below="@id/remove_button" 
6

在Android佈局資源XML源文件:

"@+id/anyId":添加新的ID

"@id/anyId":指現有的ID

應使用"@id/anyId"只有當 「anyId」 已添加到R.java類使用"@+id/anyId"

相關問題