Android自定义 ProgressDialog的实现

本文介绍了如何自定义Android系统的ProgressDialog,通过设置style.xml和customprogressdialog.xml文件来改变其样式。作者强调博客中的代码均可运行,并展示了修改前后的区别,以解决ProgressDialog无法正确释放的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

算是半改这篇文章吧,稍作修改,有不足之处,还请指正

纯转载不运行的事我不干,我的博客都是能运行的东西,不想让人看了没结果

http://blog.csdn.net/yeqishi/article/details/7622023

看看效果图


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文件,定义自己的布局,由于我的需求只需要一个进度条以及一串显示的内容,所以布局比较简单

<?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出来了。



评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值