![](//images0.cnblogs.com/i/651487/201407/202006309287850.png)
package com.kale.codeview;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//定义一个父控件
LinearLayout layout = new LinearLayout(this);
//设置activity显示的layout
super.setContentView(layout);
//设置linearLayout的方向
layout.setOrientation(LinearLayout.VERTICAL);
//new 一个textview
final TextView show = new TextView(this);
//new 一个button
Button bn = new Button(this);
bn.setText(R.string.app_name);
//设置button的排列方式
bn.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
//添加视图
layout.addView(show);
layout.addView(bn);
bn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO 自动生成的方法存根
show.setText("Time is "+ new java.util.Date());
Toast.makeText(MainActivity.this, "CodeView", 0).show();
}
});
}
}