如何在XML中创建可重用的视图组?

时间:2014-12-25 17:39:34

标签: android android-layout android-viewgroup

我经常使用具有相同属性的相同LinearLayout。 例如:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        ... other views & viewgroups here ....
</LinearLayout>

我想定义一个XML布局组件,让我们称之为 FullScreenVerticalLinearLayout ,我将这样使用:

<FullScreenVerticalLinearLayout>
        ... other views & viewgroups here ....
</FullScreenVerticalLinearLayout>

<include><merge>标记似乎非常适合视图的可重用性和聚合,但不适用于viewgroup。有关如何做到这一点的任何线索?感谢。

1 个答案:

答案 0 :(得分:2)

通过风格。 http://developer.android.com/guide/topics/ui/themes.html 例如,将其添加到styles.xml:

<style name="LayoutStyle">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">match_parent</item>
</style>

并设置此样式:

<LinearLayout
    style="@style/LayoutStyle">
    ... other views & viewgroups here ....
</LinearLayout>