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

更多相关文章

  1. AIDL
  2. Activity的启动流程
  3. 【Android】Android用tabhost实现 界面切换,每个界面为一个独立的
  4. Android(安卓)Studio使用笔记
  5. Android面试-基础知识
  6. 获取安装的应用
  7. Viewpager Indicator中PagerSlidngTab用法(与ViewPager一起使用)
  8. 指定浏览器访问指定页面(支持UC、Opera、QQ、Dolphin、Skyfire、
  9. Android(安卓)JUnit单元测试

随机推荐

  1. android 播放rtsp协议流媒体
  2. 第四章 Android开发三大基石—Activity、
  3. 修改Android工程版本
  4. Cocos2d-x shareSDK
  5. Google工程师解析Android系统架构
  6. Android那些疑惑(1)-Application中setThe
  7. android 休眠唤醒机制分析(二) — early_
  8. Android(安卓)am 指令的使用
  9. Android(安卓)路由框架ARouter最佳实践
  10. Evernote Android(安卓)版更新,给您超便捷