android 动态 布局
16lz
2021-12-17
动态增加布局:
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));}
更多相关文章
- HTML5,js与Android(安卓)native通信
- Android中的系统栏
- Android原生开关组件
- Android音频系统之音量控制详解(Android(安卓)5.1)
- Android遍历API (1) 动画篇——克隆动画AnimationCloning
- 【Android(安卓)】零基础到飞升 | TextView(文本框)详解
- android WebView解析 调用html5页面
- Android中如何实现EditText的自动换行
- Android沉浸式状态栏,看完这篇就够了!