0

我想在xml中兩次添加相同的id(相鄰),但它一直顯示錯誤,並且不能按預期工作(我想將這兩個代碼塊放在一起而不是一個在另一個之下,我非常新的Android和沒有對這一線索,任何想法如何在我的XML中兩次添加相同的ID?

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:foo="http://schemas.android.com/apk/res/com.justin.jar" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#004D79" 
    > 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="@dimen/news_headline_padding" > 

    <com.justin.jar.utils.FontTextView 
     android:id="@+id/news_headline_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ellipsize="end" 
     android:maxLines="3" 
     android:paddingLeft="@dimen/common_left_padding" 
     android:paddingRight="5dp" 
     android:textSize="16sp" 
     foo:customFont="cabin.medium.ttf" 
     android:textColor="@color/search_autosuggest_header_text" 
     android:layout_toRightOf="@id/news_headline_image" /> 

    <com.justin.jar.utils.FontTextView 
     android:id="@+id/metadata" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/search_autosuggest_item_subtitle" 
     android:textSize="12sp" 
     android:paddingLeft="@dimen/common_left_padding" 
     android:paddingRight="5dp" 
     android:layout_alignLeft="@id/news_headline_text" 
     android:layout_toRightOf="@id/news_headline_image" 
     android:layout_below="@id/news_headline_text" 
     /> 


    <ImageView 
     android:id="@+id/news_headline_image" 
     android:layout_width="107dp" 
     android:layout_height="80dp" 
     android:background="@drawable/container_dropshadow" 
     android:paddingLeft="1dp" 
     android:paddingRight="1dp" 
     android:paddingTop="1dp" 
     android:paddingBottom="1dp" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="@dimen/common_left_padding" 
     android:contentDescription="@string/headline_image" 
     /> 
</RelativeLayout> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="@dimen/news_headline_padding" > 

    <com.justin.jar.utils.FontTextView 
     android:id="@+id/news_headline_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:ellipsize="end" 
     android:maxLines="3" 
     android:paddingLeft="@dimen/common_left_padding" 
     android:paddingRight="5dp" 
     android:textSize="16sp" 
     foo:customFont="cabin.medium.ttf" 
     android:textColor="@color/search_autosuggest_header_text" 
     android:layout_toRightOf="@id/news_headline_image" /> 

    <com.justin.jar.utils.FontTextView 
     android:id="@+id/metadata" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/search_autosuggest_item_subtitle" 
     android:textSize="12sp" 
     android:paddingLeft="@dimen/common_left_padding" 
     android:paddingRight="5dp" 
     android:layout_alignLeft="@id/news_headline_text" 
     android:layout_toRightOf="@id/news_headline_image" 
     android:layout_below="@id/news_headline_text" 
     /> 


    <ImageView 
     android:id="@+id/news_headline_image" 
     android:layout_width="107dp" 
     android:layout_height="80dp" 
     android:background="@drawable/container_dropshadow" 
     android:paddingLeft="1dp" 
     android:paddingRight="1dp" 
     android:paddingTop="1dp" 
     android:paddingBottom="1dp" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="@dimen/common_left_padding" 
     android:contentDescription="@string/headline_image" 
     /> 
</RelativeLayout> 
</FrameLayout> 

這裏是什麼,我想 http://i.stack.imgur.com/TgfZl.png

爲例

[1]:

+4

嗯...給他們不同的'ID's? – codeMagic

+0

它可能有助於理解XML不是代碼。它是數據,數據的格式由特定的規則決定。你不能簡單地重新安排「塊」,如你所願。 – Simon

+0

這就是我需要的,我怎麼去layoutwise? http://i.stack.imgur.com/TgfZl.png –

回答

0

ID的不能被多次使用w在一個XML文檔中。他們必須是獨一無二的。

+0

不,您可以在一個佈局文件中使用相同的ID, – pskink

+0

@pskink錯誤。嘗試將任何偵聽器附加到一個佈局文件中帶有多個參考點的ID,您的應用將崩潰。 –

2

From the Docs

ID

任何視圖對象可具有與其相關聯的整數ID,以唯一地識別 樹內觀。

更改爲id之一。例如

<com.justin.jar.utils.FontTextView 
android:id="@+id/news_headline_text_right" 

你可以有View s的不同resource文件相同的id,我相信,雖然我不認爲這是一個好主意。但是在樹中,它們必須是唯一的,否則當你嘗試訪問它時,Android不會知道使用哪一個,比如findViewById()

+0

其實不然:你甚至可以在一個佈局文件中擁有相同的ID,爲什麼不呢? – pskink

+0

@pskink錯誤。嘗試將任何偵聽器附加到一個佈局文件中帶有多個參考點的ID,您的應用將崩潰。 –

+0

我只是試圖將2個相關佈局塊彼此相鄰,但它們使用的是java代碼中的相同id,例如在news_headlines中,我不能在java代碼中更改該代碼,因爲我只是爲表格進行修改,電話代碼相同,因此佈局中的這些更改會在大和xlarge文件夾下進行,任何想法如何解決此問題? –

0

ID proprety必須是項目中每個對象的唯一屬性。所以如果您使用相同的ID創建兩個或更多對象,這不會產生任何錯誤,但編譯器將使用他遇到的第一個對象。無論如何,你爲什麼需要這個?

相關問題