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); } }