如何分配数据以包含xml中的布局?

时间:2011-10-14 02:53:47

标签: android xml layout include

假设我们有一个包含TextView的布局:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

  <TextView android:id="@+id/text1"
    style="@style/text_style"/>

</LinearLayout>

然后多次包含此布局:

<include android:id="@+id/info1" layout="@layout/myLayout" />
<include android:id="@+id/info2" layout="@layout/myLayout" />
<include android:id="@+id/info3" layout="@layout/myLayout" />

是否可以将文本分配到包含这些布局的xml文件中的每个TextView中? 如果没有,那么如何在运行时分配?

1 个答案:

答案 0 :(得分:1)

你可以,为此你需要识别你的布局

LinearLayout info1 = (LinearLayout)findViewById(R.id.info1);

然后使用此布局对象,您需要识别TextView

TextView text1 = (TextView)info1.findViewById(R.id.text1);
text1.setText("Your text");