2012-07-21 119 views
2

我想这个代码:如何在每个布局页面中使用标题?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" > 

<TextView android:id="@+id/logo" style="@style/logo" 
    android:layout_alignParentTop="true" 
    android:text="@string/logo" tools:context=".MainActivity" /> 

在每一个布局的开始。我怎么做,我不需要将其添加到每个布局页面?就像例如在PHP中,我会使用<?=include("header.php");?>(只是一个例子,实际上这是不好的做法,这里没关系)。谢谢。

回答

2

保存在XML文件的标题,然后包括该XML在其他布局子布局:

<include layout="@layout/headerlayout" android:id="@+id/headerLayoutid" ... /> 

headerlayout.xml是你的上述布局的名称应在res/layout定义和你喜欢在所有布局中显示为标题(layout="@layout/headerlayout"),也可以是headerLayoutid是您的标题布局标识(在其父级中),您可以在您的父级布局或代码中引用它。
您可以覆盖所有布局参数。这意味着任何android:layout_*属性都可以与标签一起使用。以下是一个示例:

<include android:layout_width="fill_parent" layout="@layout/image_holder" /> 

您可以在this page中查看更多详细信息。
编辑
如果您有关于寻找中包括的布局图,看到这个问题的问题,我希望这些帮助你:
findViewById not working for an include?
Finding an view by id?

+0

@hey请参阅我的编辑。 – hasanghaforian 2012-07-21 07:46:10

2

您可以在布局中使用<include .../>标签,以便在需要的地方重新使用标题。一个例子见here

相关问题