Android布局代码实现
周sir |
2015年4月20日 |
Android UI界面 |
0 条评论 | 1761
布局不一定只能写 XML。用 LayoutInflater 把 XML 灌成 View,或纯代码 new 出 LinearLayout / RelativeLayout,都能在运行时拼界面。关键是 inflate(resource, root, attachToRoot) 三个参数怎么配合。
一、LayoutInflater 三参数
先拿到 Inflater,再 inflate:
LayoutInflater mInflater = LayoutInflater.from(context);
ViewGroup parent;
View v = mInflater.inflate(R.layout.item_simpleadapter_view, parent, true);
View v = mInflater.inflate(R.layout.item_simpleadapter_view, parent, false);
View v = mInflater.inflate(R.layout.item_simpleadapter_view, null);
对应签名:public View inflate(int resourceId, ViewGroup root, boolean attachToRoot)
| root |
attachToRoot |
结果 |
| 非空 |
false |
从 root 取出 layout_width / layout_height 组成 LayoutParams,赋给加载出来的 View,但不 addView |
| 非空 |
true |
把加载的视图作为子 View 加进 root |
| null |
(忽略) |
直接返回创建的视图,没有父布局的 LayoutParams |
二、代码实现线性布局
new 一个垂直 LinearLayout,铺满父布局,再 add 两个 TextView,最后 setContentView:
public void setLinearLayout() {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams layoutParams = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(layoutParams);
TextView textView = new TextView(this);
textView.setGravity(Gravity.CENTER);
textView.setText("子元素0");
textView.setTextSize(20);
textView.setBackgroundColor(R.color.myblue);
textView.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
TextView textView1 = new TextView(this);
textView1.setGravity(Gravity.CENTER);
textView1.setText("子元素1");
textView1.setTextSize(20);
textView1.setBackgroundColor(Color.BLUE);
textView1.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
linearLayout.addView(textView);
linearLayout.addView(textView1);
setContentView(linearLayout);
}
三、代码实现相对布局
RelativeLayout 靠 addRule 描述谁在谁上、谁对齐谁。下面四个按钮:btn1 顶栏水平居中;btn2 在其下且左对齐;btn3 在 btn1 下方、btn2 右侧并右对齐 btn1;btn4 在 btn2 下方水平居中。
public void setRelationLayout() {
RelativeLayout rl = new RelativeLayout(this);
Button btn1 = new Button(this);
btn1.setText("----------1------------");
btn1.setId(1);
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);
// btn1 位于父 View 顶部,水平居中
rl.addView(btn1, lp1);
Button btn2 = new Button(this);
btn2.setText("|\n|\n2\n|\n|\n|");
btn2.setId(2);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.BELOW, 1);
lp2.addRule(RelativeLayout.ALIGN_LEFT, 1);
// btn2 在 btn1 下方,左边与 btn1 对齐
rl.addView(btn2, lp2);
Button btn3 = new Button(this);
btn3.setText("|\n|\n3\n|\n|\n|");
btn3.setId(3);
RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp3.addRule(RelativeLayout.BELOW, 1);
lp3.addRule(RelativeLayout.RIGHT_OF, 2);
lp3.addRule(RelativeLayout.ALIGN_RIGHT, 1);
// btn3 在 btn1 下方、btn2 右方,右边与 btn1 对齐(会拉宽)
rl.addView(btn3, lp3);
Button btn4 = new Button(this);
btn4.setText("-----------------4--------------");
btn4.setId(4);
RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
lp4.addRule(RelativeLayout.BELOW, 2);
lp4.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
// btn4 在 btn2 下方,父布局中水平居中
rl.addView(btn4, lp4);
setContentView(rl);
}
一句话总结:inflate 时 root + attachToRoot 决定「只借 LayoutParams」还是「直接挂到父布局」;纯代码布局则是 new 容器、设 LayoutParams、addView,最后 setContentView。
转载请注明来源:Android布局代码实现
我是周sir,这是我的博客。致力于分享我学会的技术和优秀的文章。微信公众号 “周sir专栏” 欢迎大家关注。