<include>标记覆盖属性</include>

时间:2012-02-21 13:35:15

标签: android android-layout android-intent android-widget

如果我有一个布局文件=&gt; res/layout/item.xml

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

>

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="John"
        />

    <TextView  
        android:id="@+id/age"
        android:layout_width="fill_parent"
        android:layout_height="26dip" 
        android:text="29" />
</LinearLayout>

我还有另一个布局文件,其内容包括以上几项=&gt; res/layout/main.xml

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

    <!--How can I override "name" and "age" attribute in the included item layout-->        

    <include android:id="@+id/student" layout="@layout/tem" />

    <include android:id="@+id/teacher" layout="@layout/item" />

    <include android:id="@+id/other" layout="@layout/item" />

</LinearLayout>

如上所述,我使用<include>标记来包含相同的布局。但是,如何覆盖 main.xml 中包含的 item.xml 中的“name”和“age”文本值包括项目??

2 个答案:

答案 0 :(得分:1)

I hope this will work.

View v=(View)findviewbyid(R.id.student);

Textview name=(TextView)v.findviewbyid(R.id.name);

Textview age=(TextView)v.findviewbyid(R.id.age);

答案 1 :(得分:-1)

按以下方式更改item.xml:

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

>

    <TextView
        android:tag="item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="John"
        />

    <TextView  
        android:tag="item_age"
        android:layout_width="fill_parent"
        android:layout_height="26dip" 
        android:text="29" />
</LinearLayout>

然后:

//find the container which hold student: 
     ViewGroup containerStudent = findViewById(R.id.student);
   //find the child:
     TextView student_name =(TextView)containerStudent.findViewWithTag("item_name");