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