2013-04-15 88 views
5

我想知道如果這是可能的設置文本:安卓:包括視圖

在佈局文件,我已經包括了一個觀點:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <include 
     layout="@layout/includedView" /> 

</LinearLayout> 

這includedView包含此:

<?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="48dip" > 

    <ImageView 
    ... 
    /> 

    <TextView 
    .... 
    /> 

</RelativeLayout> 

我的問題是:是否有可能從包含視圖的佈局(因此來自佈局1)爲includedView內的textview設置文本?

希望我的問題很清楚,如果不是,請問。

在此先感謝!

+0

是的,有可能,您可以參考該視圖作爲整體包括佈局被複制到主佈局文件。 – Analizer

+0

是的,正如Analizer所說,這是可能的..... –

+0

但包含的佈局不是一個textview,包含的佈局包含一個textview。這很重要嗎? – Xander

回答

1

你可以做到這一點從代碼一樣的單一佈局。例如

setContentView(R.layout.first_layout); 
TextView tv = (TextView)findViewById(R.id.textview_of_second_layout); // just like single layout 
tv.setText(something); 

但我認爲這是不可能做到這一點從第一個佈局xml,因爲沒有可見的方式。 (有人糾正我,如果我錯了)

1

是的,可以的,您可以參考該視圖,因爲整個包含的佈局被複制到主佈局文件中。你可以使用它的ID作爲allways參考textview,它會被發現

+0

你從代碼或XML是什麼意思? – Xander

+1

您只能在包含的佈局中從xml進行設置,因此只能設置一次,並且在包含的每個佈局中將其設置爲相同的值,因此我建議您從代碼中設置它。 – Analizer

+0

好的,你可以使用findViewById()還是必須使用includedView.findViewById()? – Xander