android 新建布局,Android使用布局作为创建多个布局实例的模板

你可以轻松地做到这一点,你只需要打破它。首先加载要插入动态视图的布局。然后,您可以充实您的子视图,并根据需要填充多次。然后将视图添加到父版本,最后将活动的内容视图设置为父视图。

这里有一个例子:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

LinearLayout parent = (LinearLayout) inflater.inflate(R.layout.main,

null);

for (int i = 0; i < 3; i++) {

View custom = inflater.inflate(R.layout.custom, null);

TextView tv = (TextView) custom.findViewById(R.id.text);

tv.setText("Custom View " + i);

parent.addView(custom);

}

setContentView(parent);

这里是我插入的main.xml文件:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

这里是我放大,填充和动态插入的custom.xml视图:

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal" >

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

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal" >

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/ic_launcher" />

android:id="@+id/text"

android:layout_width="fill_parent"

android:layout_height="wrap_content" />

希望这个例子有所帮助!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值