Android自定义progressDialog使用系统ProgressBar与图片实现(一)系统ProgressBar


http://blog.csdn.net/wht3xr/article/details/20207005(转)

Android自定义progressDialog使用系统ProgressBar与图片实现(一)系统ProgressBar




目录(?)[+]

虽然是很简单的一个操作,不过我觉得对于初学者们还是有所帮助的。

大家都知道,网络请求数据的时候需要一段时间,尤其是使用移动数据网络的时候,所以我们的程序需要一个Dialog向用户反馈正在请求网络,但由于系统自带的ProgressDialog看起来实在是有点幽幽的蛋疼,所以还是动手自定义一个吧。

使用系统ProgressBar

首先布局一下MyProgressDialog

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout     
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="horizontal">  
  7.   
  8.     <ProgressBar  
  9.         android:id="@+id/loadbar"  
  10.         style="?android:attr/progressBarStyleLarge"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content" />  
  13.   
  14.         <TextView     
  15.             android:id="@+id/id_tv_loadingmsg"  
  16.             android:layout_width="wrap_content"  
  17.             android:layout_height="wrap_content"  
  18.             android:layout_gravity="center_vertical"  
  19.             android:textSize="20sp"/>  
  20. </LinearLayout>   

然后继承Dialog完成我们的MyProgressDialog类

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public class MyProgressDialog extends Dialog {  
  2.     private Context context = null;  
  3.     private static MyProgressDialog progressDialog = null;  
  4.     public MyProgressDialog(Context context){  
  5.         super(context);  
  6.         this.context = context;  
  7.     }  
  8.     public MyProgressDialog(Context context, int theme) {  
  9.         super(context, theme);  
  10.     }  
  11.     public static MyProgressDialog createDialog(Context context){  
  12.         progressDialog = new MyProgressDialog(context,R.style.myprogressDialog);  
  13.         progressDialog.setContentView(R.layout.my_progressdialog);    
  14.         progressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;  
  15.         return progressDialog;  
  16.     }  
  17.     public void onWindowFocusChanged(boolean hasFocus){  
  18.         if (progressDialog == null){  
  19.             return;  
  20.         }  
  21.         View progressBar = (ProgressBar) progressDialog.findViewById(R.id.loadbar);  
  22.         progressBar.setVisibility(View.VISIBLE);  
  23.     }  
  24.     /** 
  25.     * setTitile 标题 
  26.     * @param strTitle 
  27.     * 
  28.     */  
  29.     public MyProgressDialog setTitile(String strTitle){  
  30.         return progressDialog;  
  31.     }  
  32.     /** 
  33.     * setMessage 提示内容 
  34.     * @param strMessage 
  35.     */  
  36.     public MyProgressDialog setMessage(String strMessage){  
  37.         TextView tvMsg = (TextView)progressDialog.findViewById(R.id.id_tv_loadingmsg);  
  38.         if (tvMsg != null){  
  39.             tvMsg.setText(strMessage);  
  40.         }  
  41.             return progressDialog;  
  42.     }  
  43. }  

到这里我们的MyProgressDialog就算完成了,不过这个还不够,需要对Dialog的style进行相应的调整

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <style name="myprogressDialog" parent="@android:Theme.Dialog">  
  2.         <item name="android:windowFrame">@null</item><!--边框-->  
  3.         <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->  
  4.         <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item><!--输入法影响-->  
  5.         <item name="android:windowBackground">@android:color/transparent</item><!--背景透明-->  
  6.         <item name="android:windowNoTitle">true</item>  
  7.     </style>  

这下一切都完成了,放到Activity里面使用吧,下面附上系统自带与现在的对比图
系统自带:        自定义:

下面一篇再使用一组图片完成更美观的ProgressDialog


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值