删掉了main.xml文件
首先使用线性布局
LinearLayout linearLayout=new LinearLayout(this);
设置布局方向:
linearLayout.setOrientation(LinearLayout.VERTICAL);
布局的参数:填充整个窗口的宽和高
LinearLayout.LayoutParams layoutParams=new LinearLayout.LinearParams(
ViewGroup.LinearParams.HILL_PARENT,ViewGroup.LinearParams.FILL_PARENT
);
建立一个textview的布局参数:
ViewGroup.LayoutParams textViewParams=new ViewParams.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT)
建立一个textview
TextView textView=new TextView(this);
textView.setText(R.string.app_name);
textView.setId(20);
往容器里填加控件:
linearLayout.addView(text.View,textViewParams);
setContentView(linearLayout,layoutParams);
只有界面在运行期。随着运行期的某些条件而变化的
实验:
private static final int HELLO_TV_ID = 10;
private LayoutParams params;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
LinearLayout linearLayout = new LinearLayout(this);
TextView textView = new TextView(this);
textView.setText(R.string.hello);
textView.setId(HELLO_TV_ID);
params = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
linearLayout.addView(textView, params);
params = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.setContentView(linearLayout,params);
}