把孩子放在里面包括标签

时间:2014-11-09 22:21:24

标签: android android-xml

所以根据这个 - http://developer.android.com/training/improving-layouts/reusing-layouts.html,我可以使用include标签重用布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="@color/app_bg"
android:gravity="center_horizontal">

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

<TextView android:layout_width=”match_parent”
          android:layout_height="wrap_content"
          android:text="@string/hello"
          android:padding="10dp" />

...

</LinearLayout>

是否可以将元素放在include标记内。

例如:

<include layout="@layout/titlebar">
    <Button/>
</include>

如果没有,我怎么能得到类似的效果。

我的目标是能够将include标记作为根元素。

1 个答案:

答案 0 :(得分:1)

要从xml中添加元素,您必须在titlebar.xml中的<include> </include> 而不是中添加它们。 否则你可以从代码中添加你的元素:

ViewGroup layout = (ViewGroup) findViewById(R.id.your_title_bar_id);
Button btn = new Button(this);
btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
layout.addView(btn );
相关问题