MainActivity。java  代码如下:
package com.example.andrew;import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
public TextView text2;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frameLayout=new FrameLayout(this);
frameLayout.setBackgroundDrawable(this.getResources().
getDrawable(TRIM_MEMORY_UI_HIDDEN));//设置背景
setContentView(frameLayout);
TextView text1=new TextView(this);
text1.setText("在代码中控制UI界面");
text1.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);
text1.setTextColor(Color.CYAN);
frameLayout.addView(text1);

text2=new TextView(this);
text2.setText("单击进入游戏界面。");
text2.setTextSize(TypedValue.COMPLEX_UNIT_PX, 24);
text2.setTextColor(Color.GREEN);
LayoutParams params=new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity=Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL;
text2.setLayoutParams(params);
text2.setOnClickListener(new OnClickListener(){
@ Override
public void onClick(View v){
new AlertDialog.Builder(MainActivity.this).setTitle("系统提示")
.setMessage("游戏有风险")
.setNegativeButton("确定",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.i("android","进入游戏");
}
}).setNegativeButton("退出",
new  DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.i("android", "退出游戏");
finish();
}
}).show();
}
});


frameLayout.addView(text2);


} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}

解决方案 »

  1.   

    有几个问题:
    1.this.getResources().getDrawable(TRIM_MEMORY_UI_HIDDEN),这里的TRIM_MEMORY_UI_HIDDEN未定义,所以你的apk挂掉了。这个参数一般都是R.drawable.****,****就是你放在res/drawable/目录下的****.png图片。
    2.frameLayout.setBackgroundDrawable()这个方法已经不推荐用了,建议用setBackground()。
    3.你用FrameLayout,但是两个TextView之间又没有设置padding,导致两个text叠加显示在一起。建议换LinearLayout垂直布局,或者还是用FrameLayout,设置TextView的间距。