有时候我们需要在游戏或应用中用一些符合我们样式的提示框(AlertDialog)

以下是我在开发一个小游戏中总结出来的.希望对大家有用.

先上效果图:

下面是用到的背景图或按钮的图片

经过查找资料和参考了一下例子后才知道,要实现这种效果很简单.就是在设置alertDialog的contentView.

以下的代码是写在Activity下的,代码如下:

public boolean onKeyDown(int keyCode, KeyEvent event) {
// 如果是返回键,直接返回到桌面
if(keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME){
showExitGameAlert();
} return super.onKeyDown(keyCode, event);
}
private void showExitGameAlert() {
final AlertDialog dlg = new AlertDialog.Builder(this).create();
dlg.show();
Window window = dlg.getWindow();
// *** 主要就是在这里实现这种效果的.
// 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
window.setContentView(R.layout.shrew_exit_dialog);
// 为确认按钮添加事件,执行退出应用操作
ImageButton ok = (ImageButton) window.findViewById(R.id.btn_ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
exitApp(); // 退出应用...
}
}); // 关闭alert对话框架
ImageButton cancel = (ImageButton) window.findViewById(R.id.btn_cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dlg.cancel();
}
});
}

以下的是layout文件,定义了对话框中的背景与按钮.点击事件在Activity中添加.

文件名为 : shrew_exit_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:Android="http://schemas.android.com/apk/res/android"
   android:layout_height="wrap_content"
  android:layout_width="wrap_content"> <!-- 退出游戏的背景图 -->
<ImageView android:id="@+id/exitGameBackground"
   android:layout_centerInParent="true"
  android:layout_height="wrap_content"
  android:layout_width="wrap_content"
   android:src="@drawable/bg_exit_game" /> <!-- 确认按钮 -->
<ImageButton android:layout_alignBottom="@+id/exitGameBackground"
   android:layout_alignLeft="@+id/exitGameBackground"
   android:layout_marginBottom="30dp"
   android:layout_marginLeft="35dp"
   android:id="@+id/btn_ok"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:background="@drawable/btn_ok" /> <!-- 取消按钮 -->
<ImageButton android:layout_alignBottom="@+id/exitGameBackground"
  android:layout_alignRight="@+id/exitGameBackground"
   android:layout_marginBottom="30dp"
   android:layout_marginRight="35dp"
   android:id="@+id/btn_cancel"
   android:layout_height="wrap_content"
   android:layout_width="wrap_content"
   android:background="@drawable/btn_cancel" />
</RelativeLayout>

最新文章

  1. Bootstrap 简介
  2. iOS_屏幕截图
  3. 【POJ 2923】Relocation(状压DP+DP)
  4. [实变函数]5.6 Lebesgue 积分的几何意义 $\bullet$ Fubini 定理
  5. CI在CentOS中的部署与实践LNMP
  6. WordPress Contact Form 7插件任意文件上传漏洞
  7. PC问题-使用BAT方法设置IP地址
  8. iOS 颜色选择器 仿ps 调色板
  9. PHP实现单击“添加”按钮增加一行表单项,并将所有内容插入到数据库中
  10. UML--核心元素之包
  11. 手动安装英特尔&#174; 凌动™ Android* x86 模拟器映像
  12. day2--课前考试题
  13. windows server 2016远程桌面进去,英文系统修改语言
  14. 如何成为一名优秀的web前端工程师
  15. pycharm 使用教程
  16. SPSS-方差分析
  17. LINUX 查找tomcat日志关键词
  18. bootstrap 的模态框的宽与高设置
  19. Linux samba 服务的配置
  20. PCB标识说明

热门文章

  1. javascript保存excel
  2. java I/O系统总结
  3. windows下python3.6版本安装pygame
  4. 2018春招-今日头条笔试题-第四题(python)
  5. artTemplate-优秀的前端模板引擎
  6. android 解决studio生成aar包并在其他工程引用aar包的坑,不需要任何gradle配置
  7. android学习-Adapter适配器进阶
  8. 反转ListBox的ListBoxItem(控件级别,不是数据的反转)
  9. ZooKeeper:架构和算法
  10. C++中对象模型