Android-自定义Dialog

来源:互联网 发布:ih5制作软件 编辑:程序博客网 时间:2024/05/22 20:28
本篇博文来分享一个也是开发中经常需要用到的功能-自定义对话框,这里我用到了Android中的图形资源shape,具体使用方法,各位看代码吧,Android有多钟图形资源,后面小巫也会总结分享出来,方便各位使用。
我们来看看自定义Dialog的具体步骤吧:
1.修改系统默认的Dialog样式(风格、主题)
2.自定义Dialog布局文件
3.可以自己封装一个类,继承自Dialog或者直接使用Dialog类来实现,为了方便以后重复使用,建议自己封装一个Dialog类
源码下载:http://download.csdn.net/detail/wwj_748/7261031
有兴趣的朋友可以加本人创建的群,里面有丰富的学习资源哦:299402133(移动开发狂热者群)
效果图:
具体实现代码如下:

1. 修改样式
/04_CustomDialog/res/values/styles.xml
添加以下代码:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <!-- 对话框主题 -->  
  2.    <style name="DialogTheme" parent="@android:style/Theme.Dialog">  
  3.        <item name="android:windowBackground">@android:color/transparent</item>  
  4.        <item name="android:windowNoTitle">true</item>  
  5.    </style>  

2. 自定义Dialog
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.wwj.custom.dialog;  
  2.   
  3. import android.app.Dialog;  
  4. import android.content.Context;  
  5. import android.content.res.Resources;  
  6. import android.util.DisplayMetrics;  
  7. import android.view.Gravity;  
  8. import android.view.Window;  
  9. import android.view.WindowManager;  
  10.   
  11. /** 
  12.  * 自定义对话框 
  13.  *  
  14.  * @author wwj 
  15.  *  
  16.  */  
  17. public class CustomDialog extends Dialog {  
  18.     private static int default_width = 160// 默认宽度  
  19.     private static int default_height = 120;// 默认高度  
  20.   
  21.     public CustomDialog(Context context) {  
  22.         super(context);  
  23.     }  
  24.   
  25.     public CustomDialog(Context context, int layout, int style) {  
  26.         this(context, default_width, default_height, layout, style);  
  27.     }  
  28.   
  29.     public CustomDialog(Context context, int width, int height, int layout,  
  30.             int style) {  
  31.         super(context, style);  
  32.         // 设置内容  
  33.         setContentView(layout);  
  34.         // 设置窗口属性  
  35.         Window window = getWindow();  
  36.         WindowManager.LayoutParams params = window.getAttributes();  
  37.         // 设置宽度、高度、密度、对齐方式  
  38.         float density = getDensity(context);  
  39.         params.width = (int) (width * density);  
  40.         params.height = (int) (height * density);  
  41.         params.gravity = Gravity.CENTER;  
  42.         window.setAttributes(params);  
  43.   
  44.     }  
  45.   
  46.     /** 
  47.      * 获取显示密度 
  48.      *  
  49.      * @param context 
  50.      * @return 
  51.      */  
  52.     public float getDensity(Context context) {  
  53.         Resources res = context.getResources();  
  54.         DisplayMetrics dm = res.getDisplayMetrics();  
  55.         return dm.density;  
  56.     }  
  57. }  


3. 自定义布局
/04_CustomDialog/res/layout/dialog_layout.xml
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:background="@drawable/dialog_bg"  
  5.     android:gravity="center"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <ProgressBar  
  9.         style="@style/DialogTheme"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/message"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_marginTop="5dp"  
  18.         android:text="正在执行..." />  
  19.   
  20. </LinearLayout>  

布局文件中用到了一个图像资源:
/04_CustomDialog/res/drawable/dialog_bg.xml
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle" >  
  4.   
  5.     <corners android:radius="10dp" />  
  6.   
  7.     <solid android:color="#55000000" />  
  8.   
  9. </shape>  


4. 显示自定义对话框
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.wwj.custom.dialog;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. /** 
  7.  * 1.修改系统默认的Dialog样式(风格、主题) 
  8.  *  
  9.  * 2.自定义Dialog布局文件 
  10.  *  
  11.  * 3.可以自己封装一个类,继承自Dialog或者直接使用Dialog类来实现,为了方便以后重复使用,建议自己封装一个Dialog类 
  12.  *  
  13.  * @author wwj 
  14.  *  
  15.  */  
  16. public class MainActivity extends Activity {  
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.   
  23.         CustomDialog customDialog = new CustomDialog(this,  
  24.                 R.layout.dialog_layout, R.style.DialogTheme);  
  25.         customDialog.show();  
  26.     }  
  27.   
  28. }  
0 0