自定义ProgressDialog

Android 2016-04-07

起步

主要实现方法为使用Dialog,将布局改写为ProgressDialog

编码

先编写主要界面布局my_lodding_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:wheel="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/dialog_view"
    >
    <com.pnikosis.materialishprogress.ProgressWheel
        android:id="@+id/progress_wheel"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_centerHorizontal="true"
        wheel:matProg_barColor="@color/mainColor"
        wheel:matProg_progressIndeterminate="true"
        />
</LinearLayout>

其中com.pnikosis.materialishprogress.ProgressWheel 为github开源组件,下载地址

定义dialog的样式,这里我们定义为圆形。bg_lodding.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    >
    <solid android:color="#FFFFFF" />
    <corners android:radius="360dip" />
    <stroke
        android:width="0.5dip"
        android:color="#B5B5B5" />
    <padding
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp" />
</shape>

然后定义在style.xml中定义样式

<style name="loading_dialog" parent="android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@drawable/bg_lodding</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>

定义工具类回去dialog

package com.masterzhang.www.util;

import android.app.Activity;
import android.app.Dialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.masterzhang.www.ecourier.R;

/**
 * 作者:张华焱 on 2016/4/7 18:22
 * 邮箱:huayanfighting@sina.com
 * 功能:得到ProgressDialog
 */
public class LoddingDialog {
    private Activity activity;
    public LoddingDialog( Activity activity )
    {
        this.activity = activity;
    }
    public Dialog getDialog()
    {
        Dialog loadingDialog = new Dialog(activity, R.style.loading_dialog);
        LayoutInflater inflater = LayoutInflater.from(activity);
        View v = inflater.inflate(R.layout.my_lodding_dialog, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载布局
        loadingDialog.setCancelable(false);// 不可以用“返回键”取消
        loadingDialog.setCanceledOnTouchOutside(false);
        loadingDialog.setContentView(layout);// 设置布局
        return loadingDialog;
    }
}

本文由 zhanghuayan 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

赏个馒头吧