Android 动态创建各种控件及位置设定
来源:互联网 发布:nginx绑定多个域名 编辑:程序博客网 时间:2024/05/29 18:16
Android 动态创建各种控件及位置设定,以相对布局为例。
位置设定主要由RelativeLayout.LayoutParams控制
1. 创建TextView
final TextView title = new TextView(this);title.setText(Html.fromHtml("<br><b>Your Title</b><br><br>"));//加粗字体title.setId(1);RelativeLayout.LayoutParams titleLP = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);titleLP.leftMargin = 10;titleLP.topMargin = 10;
2. 创建button
final Button activate = new Button(this);activate.setText("Activate");activate.setId(10);RelativeLayout.LayoutParams activeLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);activeLP.topMargin = 80;activeLP.addRule(RelativeLayout.BELOW, 8);activeLP.addRule(RelativeLayout.CENTER_HORIZONTAL);
3. 创建radiobutton
final RadioGroup rg = new RadioGroup(this.getApplicationContext());rg.setId(2);RadioButton pk = new RadioButton(this);pk.setText("Radio 1");pk.setId(3);pk.setChecked(true);RadioButton lf = new RadioButton(this);lf.setText("Radio 2");lf.setId(4);rg.addView(pk);rg.addView(lf);
final CheckBox sslCB = new CheckBox(this);sslCB.setId(7);sslCB.setChecked(false);RelativeLayout.LayoutParams sslCBLP = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);sslCBLP.leftMargin=20;sslCBLP.topMargin=60;sslCBLP.addRule(RelativeLayout.BELOW, 5);sslCBLP.addRule(RelativeLayout.RIGHT_OF, 6);sslCBLP.addRule(RelativeLayout.ALIGN_BASELINE, 6);
5. 创建layout,并且把控件加载进去
layout = new RelativeLayout(this);requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);super.setContentView(layout);
layout.addView(title, titleLP);//加载TextView,其他控件同样格式加载
0 0
- Android 动态创建各种控件及位置设定
- Android 动态创建控件
- Android开发之动态创建布局文件及控件
- c#动态设置控件位置及大小
- 动态创建控件及操作控件
- android各种声音类型级数设定及默认值
- android 动态改变控件位置和大小
- 动态android设计控件的位置
- android 动态改变控件位置和大小
- Android动态添加控件约束位置
- android 动态改变控件位置和大小
- Android动态改变控件的位置
- android 中动态创建控件
- android 中动态创建控件
- android 中动态创建控件
- android 中动态创建控件
- android动态设置控件的各种属性
- android控件相对位置及长度单位
- gen_entitlements.py: No such file or directory
- Draw Call Batching
- 数据库基本操作
- eclipse PermGen Space 内存溢出
- 全角和半角
- Android 动态创建各种控件及位置设定
- python网页解析利器——BeautifulSoup
- 一个关于动态多维数组的问题
- Mac 下Versions的 svn无法上传 .a 文件的问题
- BZOJ 2435 NOI2011 道路修建 BFS/DFS
- Spring持久层(四) iBatis
- openfire外部数据库配置
- Guake terminal with mlti-monitors
- 左图片右文字的组合