http://blog.csdn.net/wht3xr/article/details/20207005(转)
虽然是很简单的一个操作,不过我觉得对于初学者们还是有所帮助的。
大家都知道,网络请求数据的时候需要一段时间,尤其是使用移动数据网络的时候,所以我们的程序需要一个Dialog向用户反馈正在请求网络,但由于系统自带的ProgressDialog看起来实在是有点幽幽的蛋疼,所以还是动手自定义一个吧。
使用系统ProgressBar
首先布局一下MyProgressDialog
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <ProgressBar
- android:id="@+id/loadbar"
- style="?android:attr/progressBarStyleLarge"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView
- android:id="@+id/id_tv_loadingmsg"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_vertical"
- android:textSize="20sp"/>
- </LinearLayout>
然后继承Dialog完成我们的MyProgressDialog类
- public class MyProgressDialog extends Dialog {
- private Context context = null;
- private static MyProgressDialog progressDialog = null;
- public MyProgressDialog(Context context){
- super(context);
- this.context = context;
- }
- public MyProgressDialog(Context context, int theme) {
- super(context, theme);
- }
- public static MyProgressDialog createDialog(Context context){
- progressDialog = new MyProgressDialog(context,R.style.myprogressDialog);
- progressDialog.setContentView(R.layout.my_progressdialog);
- progressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
- return progressDialog;
- }
- public void onWindowFocusChanged(boolean hasFocus){
- if (progressDialog == null){
- return;
- }
- View progressBar = (ProgressBar) progressDialog.findViewById(R.id.loadbar);
- progressBar.setVisibility(View.VISIBLE);
- }
- /**
- * setTitile 标题
- * @param strTitle
- *
- */
- public MyProgressDialog setTitile(String strTitle){
- return progressDialog;
- }
- /**
- * setMessage 提示内容
- * @param strMessage
- */
- public MyProgressDialog setMessage(String strMessage){
- TextView tvMsg = (TextView)progressDialog.findViewById(R.id.id_tv_loadingmsg);
- if (tvMsg != null){
- tvMsg.setText(strMessage);
- }
- return progressDialog;
- }
- }
到这里我们的MyProgressDialog就算完成了,不过这个还不够,需要对Dialog的style进行相应的调整
- <style name="myprogressDialog" parent="@android:Theme.Dialog">
- <item name="android:windowFrame">@null</item><!--边框-->
- <item name="android:windowIsFloating">true</item><!--是否浮现在activity之上-->
- <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item><!--输入法影响-->
- <item name="android:windowBackground">@android:color/transparent</item><!--背景透明-->
- <item name="android:windowNoTitle">true</item>
- </style>
这下一切都完成了,放到Activity里面使用吧,下面附上系统自带与现在的对比图
系统自带: 自定义: