【android开发】之【android动态布局方法总结】

本文深入探讨了Android中动态布局的实现方法,包括绝对布局、相对布局和线性布局的使用,以及如何通过LayoutInflater动态添加布局。文章还提供了实例代码,帮助开发者更好地理解和应用这些布局特性。

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

android动态布局方法总结

//绝对布局
AbsoluteLayout abslayout=new AbsoluteLayout (this);
setContentView(abslayout);
Button btn1 = new Button(this);
btn1.setText(”this is a button”);
btn1.setId(1);
AbsoluteLayout.LayoutParams lp1 =
new AbsoluteLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0,100);
abslayout.addView(btn1, lp1);

//相对布局
RelativeLayout relativeLayout = new RelativeLayout(this);
setContentView(relativeLayout);
AbsoluteLayout abslayout=new AbsoluteLayout (this);
RelativeLayout.LayoutParams lp1 = newRelativeLayout.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);

//线性布局
LinearLayout ll = new LinearLayout(this);
EditText et = new EditText();
ll.addView(et);
//动态添加布局的方法1.               

LinearLayout  linearlayout = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null);               

setContentView(linearlayout);               

LinearLayout  linearlayout2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,linearlayout);               

 //这样 main2 作为 main1的子布局 加到了 main1的 根节点下

//动态添加布局的方法2 addView.                       

LinearLayout  linearlayout  = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null);               

setContentView(linearlayout);               

LinearLayout  linearlayout2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,null);               

linearlayout.addView(linearlayout2);


转自:http://wenzhutech.diandian.com/post/2011-12-17/12930666



相关文章:

【Android动态布局】之【LayoutInflater的使用】

http://blog.youkuaiyun.com/conanyang/article/details/7496335

【android动态布局】之【LayoutInflater(inflate )的使用】

http://blog.youkuaiyun.com/conanyang/article/details/7542930

【Android动态布局】之【动态插入控件】

http://blog.youkuaiyun.com/conanyang/article/details/749548

【Android动态布局】之【ListView动态加载数据】

http://blog.youkuaiyun.com/conanyang/article/details/7527486


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值