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

相关文章

Java实现自定义table宽高的示例代码

《Java实现自定义table宽高的示例代码》在桌面应用、管理系统乃至报表工具中,表格(JTable)作为最常用的数据展示组件,不仅承载对数据的增删改查,还需要配合布局与视觉需求,而JavaSwing... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

一文详解Java Stream的sorted自定义排序

《一文详解JavaStream的sorted自定义排序》Javastream中的sorted方法是用于对流中的元素进行排序的方法,它可以接受一个comparator参数,用于指定排序规则,sorte... 目录一、sorted 操作的基础原理二、自定义排序的实现方式1. Comparator 接口的 Lam

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

spring-gateway filters添加自定义过滤器实现流程分析(可插拔)

《spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔)》:本文主要介绍spring-gatewayfilters添加自定义过滤器实现流程分析(可插拔),本文通过实例图... 目录需求背景需求拆解设计流程及作用域逻辑处理代码逻辑需求背景公司要求,通过公司网络代理访问的请求需要做请

CSS3 布局样式及其应用举例

《CSS3布局样式及其应用举例》CSS3的布局特性为前端开发者提供了无限可能,无论是Flexbox的一维布局还是Grid的二维布局,它们都能够帮助开发者以更清晰、简洁的方式实现复杂的网页布局,本文给... 目录深入探讨 css3 布局样式及其应用引言一、CSS布局的历史与发展1.1 早期布局的局限性1.2