2017-02-07 45 views
-1

我有此包括識別內部的視圖包括

enter image description here

我不得不「包括」這個「包括」兩次,在相同的佈局。

enter image description here

我怎樣才能進去RL1 TextView的? 我想我能做到這一點:

RelativeLayout layout1 = (RelativeLayout) findViewById(R.id.RL1); 
TextView textView = (TextView)layout1.findViewById(R.id.textViewId); 

但這樣看來,我得到的TextView的同一實例,無論從RL1和RL2。

這是一個完整的佈局:

<LinearLayout> 

    <RelativeLayout   
     android:id="@+id/RL1"> 
     <include layout="@layout/game_area"/> 
    </RelativeLayout> 


    <RelativeLayout   
     android:id="@+id/RL2"> 
     <include layout="@layout/game_area"/> 
    </RelativeLayout> 

</LinearLayout> 

預先感謝您

+0

做其他'include'有不同的'@ + id'? – WenChao

+0

將'@ + id'添加到'include'標籤 – WenChao

+0

對不起。還有一個錯誤。我做的工作方式。 謝謝你,對不起,浪費你的時間:) – MDP

回答

0

當您搜索的ID,你總能找到第一個項目,所以第二個部件是隱藏的。

但是,它可以解決

<include> -- id1 
-- stuff 
</include> 
<include> -- id2 
-- stuff 
</include> 

因此,我們可以找到子元素,首先查找ID2/ID1。

View include_1 = findViewById(R.id.id1); 
View include_2 = findViewById(R.id.id2); 

最後

include_2.findViewById(R.id.elementx); 

編號:https://stackoverflow.com/a/10821976/5707364

+0

對不起。還有一個錯誤。我做的工作方式。謝謝你,對不起,浪費你的時間:) – MDP

+0

根據你的問題,你可以通過以下方式獲得RL1中的TextView:和findViewById(R.id.id2).findViewById(R.id.textViewId); –

0
<LinearLayout> 

<RelativeLayout> 
    <include android:id="@+id/RL1" layout="@layout/game_area"/> 
</RelativeLayout> 


<RelativeLayout> 
    <include android:id="@+id/RL2" layout="@layout/game_area"/> 
</RelativeLayout> 

你shuold添加idinclude標籤

+0

不好意思。還有一個錯誤。我做的工作方式。謝謝你,遺憾浪費你的時間:) – MDP