Android布局代码实现
代码实现线型布局和相对布局以入LayoutInflater使用
LayoutInflater介绍
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)
<1>如果设置了ViewGroup root参数,且attachToRoot设置为false的话,
则会从root中得到由layout_width和layout_height组成的LayoutParams,
就会对我们加载的视图View设置该LayoutParams。
<2>如果设置了ViewGroup root参数,且attachToRoot设置为true的话,
则将我们加载的视图做为子视图添加到root视图中。
<3>如果我们ViewGroup root设置为空的话,就直接返回我们创建的视图;
代码实现线型布局和相对布局
public void setLineraLayout() { LinearLayout lineraLayout = newLinearLayout(this); lineraLayout.setOrientation(LinearLayout.VERTICAL); LayoutParams layoutParams = newLinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); lineraLayout.setLayoutParams(layoutParams); TextView textView = newTextView(this); textView.setGravity(Gravity.CENTER); textView.setText("子元素0"); textView.setTextSize(20); textView.setBackgroundColor(R.color.myblue); textView.setLayoutParams(newLinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); TextView textView1 = newTextView(this); textView1.setGravity(Gravity.CENTER); textView1.setText("子元素1"); textView1.setTextSize(20); textView1.setBackgroundColor(Color.BLUE); textView1.setLayoutParams(newLinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); lineraLayout.addView(textView); lineraLayout.addView(textView1); setContentView(lineraLayout); } publicvoidsetRelationLayout() { RelativeLayout rl = newRelativeLayout(this); Button btn1 = newButton(this); btn1.setText("----------1------------"); btn1.setId(1); RelativeLayout.LayoutParams lp1 = newRelativeLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP); lp1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); // btn1 位于父 View 的顶部,在父 View 中水平居中 rl.addView(btn1, lp1); Button btn2 = newButton(this); btn2.setText("|\n|\n2\n|\n|\n|"); btn2.setId(2); RelativeLayout.LayoutParams lp2 = newRelativeLayout.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 = newButton(this); btn3.setText("|\n|\n3\n|\n|\n|"); btn3.setId(3); RelativeLayout.LayoutParams lp3 = newRelativeLayout.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 = newButton(this); btn4.setText("-----------------4--------------"); btn4.setId(4); RelativeLayout.LayoutParams lp4 = newRelativeLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); lp4.addRule(RelativeLayout.BELOW, 2); lp4.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); // btn4 位于 btn2 的下方,在父 Veiw 中水平居中 rl.addView(btn4, lp4); setContentView(rl); }
转载请注明来源:Android布局代码实现