android 中动态创建控件
16lz
2022-06-13
public voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
// 确定界面的布局
AbsoluteLayoutabslayout= newAbsoluteLayout( this);
setContentView(abslayout);
// 创建一个button按钮
Buttonbtn1= newButton( this);
btn1.setText(” thisisabutton”);
btn1.setId(1);
// 确定这个控件的大小和位置
AbsoluteLayout.LayoutParamslp1=
newAbsoluteLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0,100);
abslayout.addView(btn1,lp1);
}
super.onCreate(savedInstanceState);
// 设置界面的布局
RelativeLayoutrelativeLayout= newRelativeLayout( this);
setContentView(relativeLayout);
// 添加一个AbsoluteLayout子布局,并给这个布局添加一个button
AbsoluteLayoutabslayout= newAbsoluteLayout( this);
abslayout.setId(11);
Buttonbtn1= newButton( this);
btn1.setText(” thisisaabslayoutbutton”);
btn1.setId(1);
AbsoluteLayout.LayoutParamslp0= newAbsoluteLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,100,0);
abslayout.addView(btn1,lp0);
// 将这个子布局添加到主布局中
RelativeLayout.LayoutParamslp1= newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp1.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
relativeLayout.addView(abslayout,lp1);
// 再添加一个子布局
RelativeLayoutrelativeLayout1= newRelativeLayout( this);
Buttonbtn2= newButton( this);
btn2.setText(” thisisarelativeLayout1button”);
btn2.setId(2);
RelativeLayout.LayoutParamslp2= newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp2.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
relativeLayout1.addView(btn2,lp2);
// 将这个布局添加到主布局中
RelativeLayout.LayoutParamslp11= newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
lp11.addRule(RelativeLayout.BELOW,11);
relativeLayout.addView(relativeLayout1,lp11);
}
super.onCreate(savedInstanceState);
// 确定界面的布局
AbsoluteLayoutabslayout= newAbsoluteLayout( this);
setContentView(abslayout);
// 创建一个button按钮
Buttonbtn1= newButton( this);
btn1.setText(” thisisabutton”);
btn1.setId(1);
// 确定这个控件的大小和位置
AbsoluteLayout.LayoutParamslp1=
newAbsoluteLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0,100);
abslayout.addView(btn1,lp1);
}
一个界面可以布置一个布局,可以多个布局一起设计
public voidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);
// 设置界面的布局
RelativeLayoutrelativeLayout= newRelativeLayout( this);
setContentView(relativeLayout);
// 添加一个AbsoluteLayout子布局,并给这个布局添加一个button
AbsoluteLayoutabslayout= newAbsoluteLayout( this);
abslayout.setId(11);
Buttonbtn1= newButton( this);
btn1.setText(” thisisaabslayoutbutton”);
btn1.setId(1);
AbsoluteLayout.LayoutParamslp0= newAbsoluteLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,100,0);
abslayout.addView(btn1,lp0);
// 将这个子布局添加到主布局中
RelativeLayout.LayoutParamslp1= newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp1.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
relativeLayout.addView(abslayout,lp1);
// 再添加一个子布局
RelativeLayoutrelativeLayout1= newRelativeLayout( this);
Buttonbtn2= newButton( this);
btn2.setText(” thisisarelativeLayout1button”);
btn2.setId(2);
RelativeLayout.LayoutParamslp2= newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lp2.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
relativeLayout1.addView(btn2,lp2);
// 将这个布局添加到主布局中
RelativeLayout.LayoutParamslp11= newRelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
lp11.addRule(RelativeLayout.BELOW,11);
relativeLayout.addView(relativeLayout1,lp11);
}
更多相关文章
- android ScrollView嵌套ListView,并且实现动态添加ListView里的It
- android 定时发送短信实现
- android页面布局
- Android(安卓)欢迎界面淡出动画效果(Animation)
- android 输入法界面显示的开关
- android sd卡数据库读取
- 给动态生成的View添加水波纹效果
- 禁止Activity+ EditText 界面默认弹出键盘
- android dialog home键