【发布时间】:2012-03-11 19:42:16
【问题描述】:
如果我有一个布局文件 => 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>
我还有另一个布局文件,其内容包括上述几个项目=> 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”文本值?
【问题讨论】:
-
你做错了。仅当您计划在同一应用的其他活动中重用确切的
item布局时,才使用include标签。 -
这个stackoverflow.com/questions/2631614/… 和这个stackoverflow.com/questions/4801710/… 即你只能覆盖layout_* 属性
-
你的意思是,即使“学生”、“教师”和“其他”项目的所有布局都相同,我仍然不能使用
?但是有什么办法可以避免重复创建相同的布局代码,只有文本值不同??
标签: android android-layout android-intent android-widget