`
weicaijin8
  • 浏览: 32702 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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 = 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);

//线性布局
LinearLayout ll = new LinearLayout(this);
EditText et = new EditText();
ll.addView(et);

//动态添加布局的方法1.
                LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null);
                setContentView(ll);
                LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,ll);
                //这样 main2 作为 main1的子布局 加到了 main1的 根节点下

//动态添加布局的方法2 addView.      
                LinearLayout ll = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main1,null);
                setContentView(ll);
                LinearLayout ll2 = (LinearLayout)this.getLayoutInflater().inflate(R.layout.main2,null);
                ll.addView(ll2);

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics