android动态生成控件

本文详细介绍了在Android应用开发中如何使用AbsoluteLayout和RelativeLayout进行界面布局,包括添加按钮、设置布局参数等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);

	//设置界面的布局
	RelativeLayout relativeLayout = new RelativeLayout(this);
	setContentView(relativeLayout);

	//添加一个AbsoluteLayout子布局,并给这个布局添加一个button
	AbsoluteLayout abslayout=new AbsoluteLayout (this);
	abslayout.setId(11);
	Button btn1 = new Button(this);
	btn1.setText(”this is a abslayout button”);
	btn1.setId(1);
	AbsoluteLayout.LayoutParams lp0 = new AbsoluteLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
	ViewGroup.LayoutParams.WRAP_CONTENT,100,0);
	abslayout.addView(btn1, lp0 );
	//将这个子布局添加到主布局中
	RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
	lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
	lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
	relativeLayout.addView(abslayout ,lp1);

	//再添加一个子布局
	RelativeLayout relativeLayout1 = new RelativeLayout(this);
	Button btn2 = new Button(this);
	btn2.setText(”this is a relativeLayout1 button”);
	btn2.setId(2);
	RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
	lp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
	lp2.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
	relativeLayout1.addView(btn2 ,lp2);

	//将这个布局添加到主布局中
	RelativeLayout.LayoutParams lp11 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
	lp11.addRule(RelativeLayout.BELOW ,11);
	relativeLayout.addView(relativeLayout1 ,lp11);
}

 原文来自:  http://www.oschina.net/code/snippet_163910_6212

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值