示例:制作一个游戏退出时的确认对话框
GameExitDialog.java
package com.test.androidtest; import android.app.Dialog; import android.content.Context; import android.view.Gravity; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.TextView; /** * 退出游戏对话框 */ public class GameExitDialog extends Dialog{ public GameExitDialog(Context context) { super(context, R.style.game_exit_dialog); setCanceledOnTouchOutside(false); Window window = getWindow(); WindowManager.LayoutParams params = window.getAttributes(); params.gravity = Gravity.CENTER; window.setAttributes(params); View view = getLayoutInflater().inflate(R.layout.game_exit_dialog, null); TextView cancelTextView = (TextView)view.findViewById(R.id.cancelTextView); cancelTextView.setOnClickListener(new android.view.View.OnClickListener(){ @Override public void onClick(View v) { GameExitDialog.this.dismiss(); } }); TextView okTextView = (TextView)view.findViewById(R.id.okTextView); okTextView.setOnClickListener(new android.view.View.OnClickListener(){ @Override public void onClick(View v) { //TODO: 处理退出逻辑 GameExitDialog.this.dismiss(); android.os.Process.killProcess(android.os.Process.myPid()); } }); setContentView(view); } }
styles.xml
<resources> <style name="game_exit_dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:background">@android:color/transparent</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:backgroundDimAmount">0.6</item> </style> </resources>
rounded_corners_view.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#ffffff" /> <corners android:radius="10dp" /> <padding android:left="40dp" android:top="5dp" android:right="40dp" android:bottom="5dp" /> </shape>
textview_style_cancle_gray.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#cccccc" /> <corners android:radius="20dp" /> <padding android:left="20dp" android:top="5dp" android:right="20dp" android:bottom="5dp" /> </shape>
textview_style_ok_blue.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#99ccff" /> <corners android:radius="20dp" /> <padding android:left="20dp" android:top="5dp" android:right="20dp" android:bottom="5dp" /> </shape>
colors.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="white">#FFFFFF</color><!--白色 --> </resources>
game_exit_dialog.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/rounded_corners_view" android:orientation="vertical" android:fadingEdge="none" style="@android:style/Theme.Dialog"> <LinearLayout android:layout_width="fill_parent" android:layout_height="30dp" android:gravity="center" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提 示" android:textSize="20sp" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:gravity="center" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="是否要退出游戏?" android:textColor="#333" android:textSize="18sp" /> </LinearLayout> <!-- 按钮 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="18dp" android:layout_marginBottom="15dp" android:gravity="center" android:orientation="horizontal" > <TextView android:id="@+id/cancelTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dp" android:background="@drawable/textview_style_cancle_gray" android:textColor="@color/white" android:text="取 消" android:textSize="18sp"/> <View android:layout_width="20dp" android:layout_height="1dp"/> <TextView android:id="@+id/okTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/textview_style_ok_blue" android:textColor="@color/white" android:text="确 定" android:textSize="18sp" /> </LinearLayout> </LinearLayout>
//显示游戏退出确认框
运行测试