阅读更多
//绝对布局
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);

更多相关文章

  1. View的invalidate()方法的源码分析
  2. Android学习札记36:一个关于onSaveInstanceState ()方法的例子
  3. android简单的动态控制软键盘的显隐
  4. Android——布局的一些动态设置
  5. Android调用系统的发邮件方法
  6. Android轮播图Banner使用方法
  7. android动态生成控件
  8. Android动态添加布局
  9. 解决Android Studio XML布局格式化无法换行的问题

随机推荐

  1. 安全星球:为什么企业都开始用SaaS了?
  2. 高新技术企业认定研发费用,及归集
  3. 云数据库RDS是什么?云数据库RDS有什么优势
  4. 软件开发人员学习ML:是不是本末倒置了?
  5. 利用自编码神经网络,检测未标记数据集中的
  6. Cortana即将停止服务,它为NLP留下了什么?
  7. shell脚本语句语法使用(超详细)
  8. 51CTO首页博客搬家入口上线!!
  9. 淘宝首页flex仿写
  10. 万能cmp程序, 有了他, 建议把其他程序全