Android自定义ProgressDialog样式

2024-05-26 11:58

本文主要是介绍Android自定义ProgressDialog样式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android系统自带的ProgressDialog样式的确不太好看,我们可以自己定义它的样式,下面看看实现

1.style.xml  progressDialog继承与Dialog,先设置一下progressDialog的风格,设置背景图片。

<style name="CustomDialog" parent="@android:style/Theme.Dialog">  <item name="android:windowFrame">@null</item>  <item name="android:windowIsFloating">true</item>  <item name="android:windowContentOverlay">@null</item>  <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>  <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>  </style>  <style name="CustomProgressDialog" parent="@style/CustomDialog">  <item name="android:windowBackground">@drawable/toast_frame</item>  <item name="android:windowNoTitle">true</item>  </style>  
2.customprogressdialog.xml文件,定义自己的布局,由于我的需求只需要一个进度条以及一串显示的内容,所以布局比较简单。

<pre name="code" class="html"><?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"  android:padding="8dp" >  <ProgressBar  android:id="@+id/loadingImageView"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_marginRight="15dp"  android:indeterminateDrawable="@drawable/progress_medium" />  <TextView  android:id="@+id/id_tv_loadingmsg"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:layout_gravity="center_vertical"  android:textColor="@android:color/white"  android:textSize="18dp" />  </LinearLayout>  

 
3.progress_medium.xml文件.旋转效果。 

<?xml version="1.0" encoding="utf-8"?>  
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"  android:drawable="@drawable/spinner_black_32"  android:fromDegrees="0"  android:pivotX="50.0%"  android:pivotY="50.0%"  android:toDegrees="360" /> 

4.CustomProgressDialog.java文件,这个是就是我们最终需要使用的progressDialog了。

package com.xxx.view;  import com.xxx.activity.R;  import android.app.Dialog;  
import android.content.Context;  
import android.view.Gravity;  
import android.widget.TextView;  public class CustomProgressDialog extends Dialog {  public CustomProgressDialog(Context context, String strMessage) {  this(context, R.style.CustomProgressDialog, strMessage);  }  public CustomProgressDialog(Context context, int theme, String strMessage) {  super(context, theme);  this.setContentView(R.layout.customprogressdialog);  this.getWindow().getAttributes().gravity = Gravity.CENTER;  TextView tvMsg = (TextView) this.findViewById(R.id.id_tv_loadingmsg);  if (tvMsg != null) {  tvMsg.setText(strMessage);  }  }  @Override  public void onWindowFocusChanged(boolean hasFocus) {  if (!hasFocus) {  dismiss();  }  }  
}  
修改的部分也就主要是这里,之前作者用静态方法去构造ProgressDialog,context无法释放,下面是修改后的代码。
在Activity里面构造showProgressDialog:我写在自己的父类里。

public void showProgress(int resID, boolean canBack) {  if (progressDialog != null) {  progressDialog.cancel();  }  progressDialog = new CustomProgressDialog(activity, getResources()  .getString(resID));  progressDialog.show();  }  

子类继承调下这个方法就show出来了。

另附圆角对话框源码:

http://download.csdn.net/detail/u010963246/8869141

这篇关于Android自定义ProgressDialog样式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1004405

相关文章

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32