如何在android中使用相同的XML布局来创建多个活动,每个活动都有自己的自定义视图

时间:2013-06-15 19:08:26

标签: java android xml

Android编码新手,但我找不到这个特定问题的答案。假设我有以下XML布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

    <view class="com.example.testapp.customView"
        android:id="@+id/drawView"
        android:layout_height="100dp"
        android:layout_width="fill_parent" />

    <Button android:id="@+id/bNormal"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="Update" />

</LinearLayout>

我想使用此布局来创建多个活动,但在每个活动中我想使用不同的customView。例如。对于一个活动,我希望自定义视图是customView1,第二个活动我希望它是customView2等。

我该怎么做呢?现在我只为每个活动都有一个单独的XML布局,这看起来非常多余。

1 个答案:

答案 0 :(得分:0)

您要么创建多个布局,要么必须以编程方式添加新视图(通过创建正确类型的对象并将其添加到父视图)而不是通过xml进行。 xml布局背后的想法是您的布局或多或少是静态的,因此xml确切地指定了您想要的内容。它本身并不意味着编程语言会发生变化。