采用代码的方式创建ui
这里只是最简单的实现类似与一个系统默认的界面
直接上代码了
1 package cn.itcast.codeui; 2 3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.view.View.OnClickListener; 7 import android.widget.Button; 8 import android.widget.LinearLayout; 9 import android.widget.TextView; 10 11 public class DemoActivity extends Activity { 12 /** Called when the activity is first created. */ 13 @Override 14 public void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 final LinearLayout ll = new LinearLayout(this); 17 ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT)); 18 ll.setOrientation(LinearLayout.VERTICAL); 19 TextView tv = new TextView(this); 20 tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 21 tv.setText(R.string.hello); 22 ll.addView(tv); 23 24 Button bt = new Button(this); 25 bt.setText("显示一个新的textview"); 26 bt.setOnClickListener(new OnClickListener() { 27 28 @Override 29 public void onClick(View v) { 30 TextView tv = new TextView(DemoActivity.this); 31 tv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 32 tv.setText("我是新的textview"); 33 ll.addView(tv); 34 } 35 }); 36 ll.addView(bt); 37 38 setContentView(ll); 39 } 40 }
位于个人日志 android基础/day08/的codeui中