1

我的 layout.xml 中有这几行:

<include android:id="@+id/promo1" android:layout_width="fill_parent"
         android:layout_height="fill_parent" layout="@layout/one_promo">
</include>
<include android:id="@+id/promo2" android:layout_width="fill_parent"
         android:layout_height="fill_parent" layout="@layout/one_promo">
</include>
<include android:id="@+id/promo3" android:layout_width="fill_parent"
         android:layout_height="fill_parent" layout="@layout/one_promo">
</include>

我想在我的 Java 代码中以编程方式执行此操作。

请问有什么帮助吗?

提前致谢

4

1 回答 1

4

如下所示使用 LayoutInflater

在您的 xml 中定义相对布局

<RelativeLayout android:id="@+id/mylayout" android:layout_width="fill_parent"
         android:layout_height="fill_parent" />

将其映射到您的 Java 代码中

RelativeLayout mynewlayout = (RelativeLayout) findById(R.id.mylayout);

使用布局充气器

LayoutInflater layoutInflater = (LayoutInflater) 
        this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    
mynewlayout.addView(0, layoutInflater.inflate(R.layout.one_promo, this, false) );
于 2011-05-11T09:17:46.820 回答