2012-04-10 99 views
1

我需要訪問包含在另一個b.xml佈局中的x.xml佈局中的視圖。 例如, 這是A.XML訪問包含的xml佈局視圖

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <Button 
      android:id="@+id/xyz" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="XYZ" /> 
</RelativeLayout> 

而且,在B.XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <include 
     android:id="@+id/a_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     layout="@layout/a" /> 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/xyz" 
     android:text="Show me below xyz" /> 
</RelativeLayout> 

我必須這樣做,在XML代碼,因爲如果我在Java中做到這一點就必須經過setContentView()然後設置TextView'label'的LayoutParams不會生效。

我想,每個人都明白我想問什麼。等待好的回覆。

謝謝大家。

右邊的圖片是我想要實現的,剩下的就是我用當前的代碼得到的。

This is what I am getting with the current xml code This is what I am trying to do

+0

您想問什麼? – 2012-04-10 07:48:32

+0

如果我已經猜到了,那麼通過它的id來訪問它:'findViewById(R.id.label)' – 2012-04-10 07:50:03

+0

你說得對,我可以使用findViewById(R.id.label)來訪問它。但我不能這樣做android:layout_below =「@ id/xyz」 – 2012-04-10 07:59:09

回答

3

在B.XML,你應該糾正:

android:layout_below="@id/xyz" 

android:layout_below="@id/a_layout" 

然後你就可以在你的代碼中使用它(在這裏,我把它放在onCreate):

setContentView(R.layout.b);  
((Button)findViewById(R.id.xyz)).setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      ((TextView)findViewById(R.id.label)).setText("clicked on XYZ button"); 
     } 
    }); 
+0

這沒有工作,無論在b.xml中看到包括佈局有android:layout_height =「fill_parent」所以它的自然,TextView將不會顯示。 – 2012-04-10 08:20:47

+0

@Trickster哦,我忘了重新檢查它 - 它必須有android:layout_height =「wrap_content」非常感謝。 – 2012-04-10 08:26:29

0

問題不在於訪問包含的佈局的View,而是因爲您無法實現這種佈局「重疊」。讓我解釋一下:如果您在a.xml的按鈕下方添加更多視圖,然後嘗試在按鈕正下方的b.xml中放置一些視圖,那麼它將使b.xml的視圖與a.xml重疊,但是尚未實施Android(但?)。所以,你可以做的唯一的事情就是把@作爲@HoàngToản的建議。

P.S.您可能會觀察到與此a + b佈局組合相同的行爲:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <Button 
      android:id="@+id/xyz" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="XYZ" /> 
    </RelativeLayout> 

    <TextView 
     android:id="@+id/label" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/xyz" 
     android:text="Show me below xyz" /> 
</RelativeLayout> 
+0

實際上,我的應用中的佈局與示例非常不同,這就是爲什麼我無法將兩者結合在一起。 這些只是兩個佈局,但在我的項目中,有15個佈局,其中包含a.xml,而a.xml不是簡單的例子,它是每個活動的標題。 – 2012-04-10 13:08:31

+0

正如我所指出的,你不能用當前的Android SDK獲得如此複雜的結構 - 'a' +'b'組合說明它(沒有'include'元素,但仍然是相同的行爲,請參閱?)。 – 2012-04-10 13:28:27