android中有时候需要用代码穿件ui
public class MainActivity extends Activity {@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);LinearLayout layout = new LinearLayout(getApplicationContext());
layout.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(getApplicationContext());
// tv.setText("代码创建UI");
//设置控件的显示特性
tv.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
tv.setText("代码创建UI");
Button bt = new Button(getApplicationContext());
bt.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
bt.setText("show");layout.addView(tv);
layout.addView(bt);
super.setContentView(layout);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "use code create UI", 1)
.show();
}
});}
}