透明自定义对话框以及进度对话框的实现

来源:互联网 发布:精微工资软件下载 编辑:程序博客网 时间:2024/05/16 18:58

来源于:http://blog.csdn.net/zoeice/article/details/7699921


项目中经常会用到对话框,但是系统的对话框又比较难看,因此就来实现自定义对话框。

办法很简单,就是定义一个Dialog,并自定义它的布局以及样式:



在res/layout里定义一个slt_cnt_type.xml,代码如下:

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/bgDialog"  
  4.     android:layout_width="240dp"  
  5.     android:layout_height="wrap_content"  
  6.     android:background="#0000"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <LinearLayout  
  10.         android:layout_width="fill_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:gravity="center_vertical" >  
  13.   
  14.         <ImageView  
  15.             android:id="@+id/ImageView01"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:src="@drawable/messagebox_warning" />  
  19.   
  20.         <TextView  
  21.             android:id="@+id/title01"  
  22.             android:layout_width="wrap_content"  
  23.             android:layout_height="wrap_content"  
  24.             android:text="@string/title"  
  25.             android:textColor="#ffe9feff"  
  26.             android:textSize="28px" />  
  27.     </LinearLayout>  
  28.   
  29.     <ScrollView  
  30.         android:id="@+id/ScrollView01"  
  31.         android:layout_width="wrap_content"  
  32.         android:layout_height="140px"  
  33.         android:background="#0000" >  
  34.   
  35.         <TextView  
  36.             android:id="@+id/TextView01"  
  37.             android:layout_width="wrap_content"  
  38.             android:layout_height="wrap_content"  
  39.             android:background="#0000"  
  40.             android:textColor="#ffe9feff" >  
  41.         </TextView>  
  42.     </ScrollView>  
  43.   
  44.     <Button  
  45.         android:id="@+id/ly1btn1"  
  46.         android:layout_width="wrap_content"  
  47.         android:layout_height="wrap_content"  
  48.         android:background="@drawable/bn9"  
  49.         android:text="Cancel"   
  50.         android:gravity="center_horizontal" >  
  51.     </Button>  
  52.   
  53. </LinearLayout>  
在res/values/style定义对话框的样式:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <style name="dialog" parent="@android:style/Theme.Dialog">  
  4.         <!-- 更换背景图片实现全透明 -->  
  5.         <item name="android:windowBackground">@drawable/alert_light</item>  
  6.         <!-- 屏幕背景不变暗 -->  
  7.         <item name="android:backgroundDimEnabled">false</item>  
  8.         <!-- 隐藏标题 -->  
  9.         <item name="android:windowNoTitle">true</item>  
  10.     </style>   
  11. </resources>  

然后再你需要弹出的对话框的地方加上:

[java] view plaincopy
  1. /* 初始化普通对话框。并设置样式 */  
  2. Dialog selectDialog = new Dialog(DialogTest.this, R.style.dialog);  
  3. selectDialog.setCancelable(true);  
  4. /* 设置普通对话框的布局 */  
  5. selectDialog.setContentView(R.layout.slt_cnt_type);  
  6.   
  7. /* +2+取得布局中的文本控件,并赋值需要显示的内容+2+ */  
  8. TextView textView01 = (TextView) selectDialog  
  9.         .findViewById(R.id.TextView01);  
  10. textView01  
  11.         .setText("光阴似箭,日月如梭。譬如流水,不舍昼夜。时光匆匆,岁月悠悠。红尘过客,流年几许。前世今生,春去秋来。愿君惜时,比肩圣人。");  
  12.   
  13. btnItem1 = (Button) selectDialog.findViewById(R.id.ly1btn1);  
  14. btnItem1.setOnClickListener(new Button.OnClickListener() {  
  15.     @Override  
  16.     public void onClick(View view) {  
  17.         selectDialog.dismiss();//隐藏对话框  
  18.     }  
  19. });  
  20. selectDialog.show();//显示对话框  

这样就完成了,是不是很简单。


然后我们实现自定义进度对话框(这里为了简单清晰,就不涉及百分比了):



先在res/layout里添加进度对话框布局文件

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/layout_root"  
  4.     android:layout_width="200px"  
  5.     android:layout_height="120px"  
  6.     android:layoutAnimation="@anim/dialog_anim_layout"  
  7.     android:orientation="horizontal" >  
  8.   
  9.     <LinearLayout  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:background="@drawable/bn9"  
  13.         android:gravity="center" >  
  14.           
  15.         <!-- 图片切换器 -->  
  16.         <ImageSwitcher  
  17.             android:id="@+id/image"  
  18.             android:layout_width="wrap_content"  
  19.             android:layout_height="wrap_content"  
  20.             android:background="@anim/plane_progress" />  
  21.     </LinearLayout>  
  22.   
  23. </LinearLayout>  

然后在res/anim定义图片切换器的帧图片:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 指定动画循环播放  oneshot设置是否循环-->  
  3. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:oneshot="false">  
  5.     <!-- 添加多个帧 -->  
  6.     <item android:drawable="@drawable/cloud4" android:duration="60" />  
  7.     <item android:drawable="@drawable/cloud3" android:duration="60" />  
  8.     <item android:drawable="@drawable/cloud2" android:duration="60" />  
  9.     <item android:drawable="@drawable/cloud1" android:duration="60" />      
  10.     <item android:drawable="@drawable/cloud" android:duration="60" />                                                       
  11. </animation-list>  

最后在代码中定义进度对话框:

[java] view plaincopy
  1. /** 
  2.  * 进度对话框 
  3.  * @author ZOEICER 
  4.  */  
  5. public class DialogProgress extends AlertDialog{  
  6.     private Handler handler = new Handler();  
  7.     /**动画*/  
  8.     AnimationDrawable anim;  
  9.     /**图片切换器*/  
  10.     ImageSwitcher imageSwitcher;  
  11.   
  12.     public DialogProgress(Context context) {  
  13.         super(context);  
  14.         // TODO Auto-generated constructor stub   
  15.     }  
  16.   
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.dialogprogress);  
  21.         imageSwitcher = (ImageSwitcher)findViewById(R.id.image);  
  22.         handler.postDelayed(new Runnable(){  
  23.             @Override  
  24.             public void run()  
  25.             {  
  26.                 /*动画为图片切换器的帧动画*/  
  27.                 anim = (AnimationDrawable)imageSwitcher.getBackground();  
  28.                 anim.start();//开始动画  
  29.             }  
  30.         }, 500);  
  31.     }   
  32.       
  33.     @Override  
  34.     public void dismiss() {  
  35.         // TODO Auto-generated method stub  
  36.         super.dismiss();  
  37.         anim.stop();//停止动画  
  38.     }  
  39. }  

然后调用:

[java] view plaincopy
  1. DialogProgress dp = new DialogProgress(DialogTest.this);  
  2. dp.show();//显示对话框  

下面附上源文件:

http://download.csdn.net/detail/zoeice/4399354

0 0
原创粉丝点击