Android 自定义AlertDialog对话框
来源:互联网 发布:阿里云邮件服务器地址 编辑:程序博客网 时间:2024/04/27 21:39
原文链接地址:http://www.pocketdigi.com/20120703/885.html
系统默认的AlertDialog,与项目的UI不统一,所以,改了一下,定义了一样式,最终效果如下图:
另外,为了尽量少改原来的代码,该类类名及相关方法名都与android.app.AlertDialog相同,使用时,原代码只需要修改导入的包名,在按钮的Listener上加上一句关闭对话框的方法即可.
先是对话框的XML布局文件:
test.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <!-- 顶部椭园边缘 -->
- <ImageView
- android:layout_width="400dp"
- android:layout_height="22dp"
- android:src="@drawable/dialog_top" >
- </ImageView>
- <!-- 中间白色背景,两个TextView,标题和内容,留一个LinearLayout,在代码中根据调用动态加上按钮 -->
- <LinearLayout
- android:layout_width="400dp"
- android:layout_height="wrap_content"
- android:background="@drawable/dialog_center"
- android:orientation="vertical" >
- <TextView android:textColor="#000000"
- android:textSize="35dp"
- android:gravity="center"
- android:id="@+id/title"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <TextView android:layout_marginLeft="20dp"
- android:layout_marginRight="10dp"
- android:id="@+id/message"
- android:layout_marginBottom="10dp"
- android:layout_marginTop="20dp"
- android:textColor="#000000"
- android:textSize="25dp"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <!-- 在LinearLayout中加按钮 -->
- <LinearLayout
- android:id="@+id/buttonLayout"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_gravity="center"
- android:gravity="center"
- android:orientation="horizontal" >
- </LinearLayout>
- </LinearLayout>
- <!-- 底部椭园边缘 -->
- <ImageView
- android:layout_marginTop="-2dp"
- android:layout_width="400dp"
- android:layout_height="22dp"
- android:src="@drawable/dialog_bottom" >
- </ImageView>
- </LinearLayout>
按钮背景,不同状态不同,可参考本博相关文章:
alertdialog_button.xml
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android" >
- <item android:state_pressed="true" android:drawable="@drawable/dialog_button_pressed" />
- <item android:state_focused="true" android:drawable="@drawable/dialog_button_pressed" />
- <item android:drawable="@drawable/dialog_button" />
- </selector>
下面就是自定义的AlertDialog类的代码:
- public class AlertDialog {
- Context context;
- android.app.AlertDialog ad;
- TextView titleView;
- TextView messageView;
- LinearLayout buttonLayout;
- public AlertDialog(Context context) {
- // TODO Auto-generated constructor stub
- this.context=context;
- ad=new android.app.AlertDialog.Builder(context).create();
- ad.show();
- //关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
- Window window = ad.getWindow();
- window.setContentView(R.layout.alertdialog);
- titleView=(TextView)window.findViewById(R.id.title);
- messageView=(TextView)window.findViewById(R.id.message);
- buttonLayout=(LinearLayout)window.findViewById(R.id.buttonLayout);
- }
- public void setTitle(int resId)
- {
- titleView.setText(resId);
- }
- public void setTitle(String title) {
- titleView.setText(title);
- }
- public void setMessage(int resId) {
- messageView.setText(resId);
- }
- public void setMessage(String message)
- {
- messageView.setText(message);
- }
- /**
- * 设置按钮
- * @param text
- * @param listener
- */
- public void setPositiveButton(String text,final View.OnClickListener listener)
- {
- Button button=new Button(context);
- LinearLayout.LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- button.setLayoutParams(params);
- button.setBackgroundResource(R.drawable.alertdialog_button);
- button.setText(text);
- button.setTextColor(Color.WHITE);
- button.setTextSize(20);
- button.setOnClickListener(listener);
- buttonLayout.addView(button);
- }
- /**
- * 设置按钮
- * @param text
- * @param listener
- */
- public void setNegativeButton(String text,final View.OnClickListener listener)
- {
- Button button=new Button(context);
- LinearLayout.LayoutParams params=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
- button.setLayoutParams(params);
- button.setBackgroundResource(R.drawable.alertdialog_button);
- button.setText(text);
- button.setTextColor(Color.WHITE);
- button.setTextSize(20);
- button.setOnClickListener(listener);
- if(buttonLayout.getChildCount()>0)
- {
- params.setMargins(20, 0, 0, 0);
- button.setLayoutParams(params);
- buttonLayout.addView(button, 1);
- }else{
- button.setLayoutParams(params);
- buttonLayout.addView(button);
- }
- }
- /**
- * 关闭对话框
- */
- public void dismiss() {
- ad.dismiss();
- }
- }
使用方法:
- final AlertDialog ad=new AlertDialog(Test.this);
- ad.setTitle("标题");
- ad.setMessage("内容sdfsafdasf内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容");
- ad.setPositiveButton("确定", new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- ad.dismiss();
- Toast.makeText(Test.this, "被点到确定", Toast.LENGTH_LONG).show();
- }
- });
- ad.setNegativeButton("取消", new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- ad.dismiss();
- Toast.makeText(Test.this, "被点到取消", Toast.LENGTH_LONG).show();
- }
- });
© 2012, 冰冻鱼. 请尊重作者劳动成果,复制转载保留本站链接! 应用开发笔记
0 0
- Android 自定义AlertDialog对话框
- android,Alertdialog,自定义对话框
- Android--自定义对话框--AlertDialog
- Android自定义Alertdialog对话框实现
- Android之AlertDialog自定义对话框
- android自定义对话框(AlertDialog)
- Android 自定义AlertDialog退出对话框
- Android 自定义AlertDialog退出对话框
- 自定义 Android 对话框 (AlertDialog) 的样式
- 完全自定义Android对话框AlertDialog的实现
- 自定义 Android 对话框 (AlertDialog) 的样式
- 自定义 Android 对话框 (AlertDialog) 的样式
- 自定义 Android 对话框 (AlertDialog) 的样式
- 自定义 Android 对话框 (AlertDialog) 的样式
- 自定义 Android 对话框 (AlertDialog) 的样式
- 自定义 Android 对话框 (AlertDialog) 的样式
- 自定义 Android 对话框 (AlertDialog) 的样式
- 自定义 Android 对话框 (AlertDialog) 的样式
- HDU 1097 A hard puzzle
- Linux开发环境搭建与使用——通过xshell远程连接ubuntu
- 周立功:我的成功可以复制
- USACO5.2.1 Snail Trails(snail)
- Js Dom 第四章
- Android 自定义AlertDialog对话框
- linux下编程动态库和静态库编译使用
- Android SDK Manager 更新
- BZOJ1018 [SHOI2008]堵塞的交通traffic(分块+并查集)
- lucio博客-个人CSDN博客App客户端发布
- C++编译错误error C2533:构造函数不能有返回类型
- 西安汽车科技学院汽车维修冬季汽车保养
- 如何判断checkbox是否被选中
- 由Map的复制问题引发对深复制和浅复制的思考