当前位置: 代码迷 >> Android >> Android自定义相仿ProgressDialog效果的Dialog
  详细解决方案

Android自定义相仿ProgressDialog效果的Dialog

热度:74   发布时间:2016-04-28 02:49:34.0
Android自定义类似ProgressDialog效果的Dialog

Android自定义类似ProgressDialog效果的Dialog.

方法如下:

1.首先准备两张自己要定义成哪样子的效果的图片和背景图片(也可以不要背景)。

如我要的效果:

?

2.定义loading_dialog.xml布局文件(这里你也可以按自己的布局效果定义,关键是要有个imageView):

?

[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/dialog_view"???
  4. ????android:orientation="vertical"??
  5. ????android:layout_width="fill_parent"???
  6. ????android:layout_height="fill_parent"??
  7. ????android:minHeight="60dp"??
  8. ????android:minWidth="180dp"??
  9. ????android:gravity="center"??
  10. ????android:padding="10dp"??
  11. ????android:background="@drawable/loading_bg">??
  12. ????<ImageView???
  13. ????????android:id="@+id/img"??
  14. ????????android:layout_width="wrap_content"???
  15. ????????android:layout_height="wrap_content"???
  16. ????????android:src="@drawable/publicloading"??
  17. ????????/>??
  18. ????<TextView???
  19. ????????android:id="@+id/tipTextView"??
  20. ????????android:layout_width="wrap_content"???
  21. ????????android:layout_height="wrap_content"??
  22. ????????android:layout_marginLeft="10dp"??
  23. ????????android:text="数据加载中……"?/>??
  24. </LinearLayout>??

?

3.定义一个loadingDialog中imageView转动的动画:loading_animation.xml

?

[html]?view plaincopy
?
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <set?android:shareInterpolator="false"?xmlns:android="http://schemas.android.com/apk/res/android">??
  3. ????<rotate???
  4. ????????android:interpolator="@android:anim/linear_interpolator"??
  5. ????????android:pivotX="50%"??
  6. ????????android:pivotY="50%"??
  7. ????????android:fromDegrees="0"??
  8. ????????android:toDegrees="+360"??
  9. ????????android:duration="1500"??
  10. ????????android:startOffset="-1"??
  11. ????????android:repeatMode="restart"??
  12. ????????android:repeatCount="-1"/>??
  13. </set>??

?

4.定义dialog的style.

?

[java]?view plaincopy
?
  1. <!--?自定义loading?dialog?-->??
  2. ????<style?name="loading_dialog"?parent="android:style/Theme.Dialog">??
  3. ????????<item?name="android:windowFrame">@null</item>??
  4. ????????<item?name="android:windowNoTitle">true</item>???
  5. ????????<item?name="android:windowBackground">@drawable/loading_bg</item>??
  6. ????????<item?name="android:windowIsFloating">true</item>??
  7. ????????<item?name="android:windowContentOverlay">@null</item>??
  8. ????</style>??



?

5.写点创建Dialog的代码,你可以自己封装成一个方法。

?

[java]?view plaincopy
?
  1. /**?
  2. ?????*?得到自定义的progressDialog?
  3. [email protected]?
  4. [email protected]?
  5. [email protected]?
  6. ?????*/??
  7. ????public?static?Dialog?createLoadingDialog(Context?context,?String?msg)?{??
  8. ??
  9. ????????LayoutInflater?inflater?=?LayoutInflater.from(context);??
  10. ????????View?v?=?inflater.inflate(R.layout.loading_dialog,?null);//?得到加载view??
  11. ????????LinearLayout?layout?=?(LinearLayout)?v.findViewById(R.id.dialog_view);//?加载布局??
  12. ????????//?main.xml中的ImageView??
  13. ????????ImageView?spaceshipImage?=?(ImageView)?v.findViewById(R.id.img);??
  14. ????????TextView?tipTextView?=?(TextView)?v.findViewById(R.id.tipTextView);//?提示文字??
  15. ????????//?加载动画??
  16. ????????Animation?hyperspaceJumpAnimation?=?AnimationUtils.loadAnimation(??
  17. ????????????????context,?R.anim.load_animation);??
  18. ????????//?使用ImageView显示动画??
  19. ????????spaceshipImage.startAnimation(hyperspaceJumpAnimation);??
  20. ????????tipTextView.setText(msg);//?设置加载信息??
  21. ??
  22. ????????Dialog?loadingDialog?=?new?Dialog(context,?R.style.loading_dialog);//?创建自定义样式dialog??
  23. ??
  24. ????????loadingDialog.setCancelable(false);//?不可以用“返回键”取消??
  25. ????????loadingDialog.setContentView(layout,?new?LinearLayout.LayoutParams(??
  26. ????????????????LinearLayout.LayoutParams.FILL_PARENT,??
  27. ????????????????LinearLayout.LayoutParams.FILL_PARENT));//?设置布局??
  28. ????????return?loadingDialog;??
  29. ??
  30. ????}??


最后来张整体的效果图:

?

?

?

转 ?http://blog.csdn.net/qjlhlh/article/details/7979179

  相关解决方案