往一个View里面添加子View的方法:

void addView(View child, int index, ViewGroup.LayoutParams params)Adds a child view with the specified layout parameters. void addView(View child, ViewGroup.LayoutParams params)Adds a child view with the specified layout parameters. void addView(View child, int index)Adds a child view. void addView(View child)Adds a child view. void addView(View child, int width, int height)Adds a child view with this ViewGroup's default layout parameters and the specified width and height. 


动态创建一个LinearLayout并添加几个TextView在里面:动态创建一个LinearLayout并添加几个TextView在里面:

Android在程序中动态生成控件,动态布局_第1张图片

代码如下:

// 定义LinearLayout的布局管理器的参数,LinearLayout.LayoutParams(int width, int height)LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);//定义TextView的布局管理参数LinearLayout.LayoutParams textParam = new LinearLayout.LayoutParams(100, 30);              //创建一个LinearLayoutfinal LinearLayout layout = new LinearLayout(this);//垂直布局layout.setOrientation(LinearLayout.VERTICAL);//背景layout.setBackgroundResource(R.drawable.hasprepared_linearlayout_bg);//子LinearLayoutLinearLayout layout2 = new LinearLayout(this);layout2.setOrientation(LinearLayout.HORIZONTAL);layout2.setPadding(60, 20, 60, 10);// 客户性质TextView customPropertyTV = new TextView(HasPreparedActivity.this);customPropertyTV.setLayoutParams(textParam);customPropertyTV.setTextSize(18);customPropertyTV.setText(getResources().getString(R.string.hasprepared_addotherincome_kehuxingzhi));layout2.addView(customPropertyTV);TextView customPropertyTV2 = new TextView(HasPreparedActivity.this);customPropertyTV2.setLayoutParams(textParam);customPropertyTV2.setText(model.getCustomProperty());customPropertyTV2.setTextSize(18);layout2.addView(customPropertyTV2);// 项目.......// 每月收入金额......                // 起始年龄.....// 结束年龄.....layout.addView(layout2, param);


 

更多相关文章

  1. android 实现自定义隐藏式菜单
  2. MUI调用自定义插件(Android)
  3. Android 动态设置全屏,退出全屏
  4. Android 自定义Dialog时出现成员变量为null的问题
  5. android 相对布局例子代码
  6. Android实现动态切换横竖屏,保存横竖屏数据(用Preference存储)
  7. Android Studio 添加百分比布局库的依赖报错 第一行代码
  8. 简单布局收缩动画

随机推荐

  1. Android 绘制太极图
  2. Android gallery详解
  3. Android插入方程技术方案分析
  4. 【源代码】一键分享各个社交平台_android
  5. android ListView详解
  6. 智能手机软件平台 Android VS iPhone OS:
  7. android 实时获取wifi信号强度
  8. Android 下载图片的问题
  9. Android:自定义DialogFragment的内容和按
  10. Android SDK Tutorial: Button onClick E