public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); FrameLayout frameLayout=new FrameLayout(this); frameLayout.setBackgroundResource(R.mipmap.bg); setContentView(frameLayout); TextView text1=new TextView(this); text1.setText("开始游戏"); text1.setTextSize(TypedValue.COMPLEX_UNIT_SP,18); text1.setTextColor(Color.rgb(17,85,114)); FrameLayout.LayoutParams params=new FrameLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); params.gravity=Gravity.CENTER; text1.setLayoutParams(params); text1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new AlertDialog.Builder(MainActivity.this).setTitle("系统提示") .setMessage("游戏有风险,进入需谨慎,真的确定要进入?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.i("桌面台球","进入游戏"); } }).setNegativeButton("退出", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.i("桌面台球","退出游戏"); finish(); } }).show(); } }); frameLayout.addView(text1); } }