动态增加布局:

private void addLayout(Context context) {LinearLayout layout = new LinearLayout(context);//设置宽高属性LayoutParams fullParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);//设置布局方向layout.setOrientation(LinearLayout.VERTICAL);//设置背景颜色layout.setBackgroundColor(getResources().getColor(R.color.black));int id = 0;//设置权重LayoutParams weightParams = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT, 1.0f);ArrayList<LinearLayout> list = new ArrayList<>();for (int i = 0; i < line; i++) {for (int j = 0; j < column; j++) {LinearLayout tempLayout = new LinearLayout(context);tempLayout.setBackgroundColor(getResources().getColor(R.color.gray));tempLayout.setId(id++);//设置idlist.add(tempLayout);}}LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT, 1.0f);params.setMargins(1, 0, 1, 0);int index = 0;for (int i = 0; i < line; i++) {LinearLayout hor = new LinearLayout(context);for (int j = 0; j < column; j++) {hor.addView(list.get(index), params);index++;}weightParams.setMargins(0, 1, 0, 1);layout.addView(hor, weightParams);}setContentView(layout, fullParams);LinearLayout linearLayout = (LinearLayout) findViewById(10);linearLayout.setBackgroundColor(getResources().getColor(R.color.red));}

更多相关文章

  1. HTML5,js与Android(安卓)native通信
  2. Android中的系统栏
  3. Android原生开关组件
  4. Android音频系统之音量控制详解(Android(安卓)5.1)
  5. Android遍历API (1) 动画篇——克隆动画AnimationCloning
  6. 【Android(安卓)】零基础到飞升 | TextView(文本框)详解
  7. android WebView解析 调用html5页面
  8. Android中如何实现EditText的自动换行
  9. Android沉浸式状态栏,看完这篇就够了!

随机推荐

  1. Android(安卓)BOOTCLASSPATH详解
  2. android保存图片到本地并可以在相册中显
  3. Android(安卓)Protect-0.Apk文件结构简介
  4. Android如何实现 相机、相册功能 + 图片
  5. android模仿qq下拉放大图片以及阻尼效果
  6. Android(安卓)对话框中的进度条 Progress
  7. Android的List Dialog实例
  8. Android(安卓)判斷機子是否聯網和聯網方
  9. android EditText限制只能输入2位小数的
  10. android的网络访问