Android开发实战总结

【Android自定义View实战】之自定义项目通用的加载等待对

2016-11-03  本文已影响855人  亮之于东

转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/52971048 【DylanAndroid的csdn博客】


在平时的Android开发中,我们难免会遇到像登陆.注册.获取数据这样的操作,而用的的网络状况不同,导致操作需要等待一定的时间,那么为了友好期间,我们需要给用户提供一个在操作完之后的一个友好的等待界面,而Android系统自带的等待对话框比较难看,而且根据项目的不同,产品经理也会有不同的要求,或者客户有特殊的要求。所以,需要我们去自己自定义一个加载等待对话框。
下面我们来做一个给中石油做的一个App中的加载等待对话框。

1.先看效果图

这里写图片描述

2.准备资源文件

3.进度条动画旋转实现

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:drawable="@mipmap/loading"
 android:pivotX="50%"
 android:pivotY="50%"
 />

4.自定义等待对话框的布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">

    <RelativeLayout
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:background="@mipmap/loading_bg">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center|bottom"
            android:orientation="vertical">

            <ProgressBar
                style="@android:style/Widget.ProgressBar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:indeterminateDrawable="@drawable/custom_progress_draw" />


            <TextView
                android:layout_marginBottom="20dp"
                android:id="@+id/tv_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="正在登录"
                android:layout_marginTop="20dp"
                android:textColor="#fff" />
        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>

5.自定义对话框代码:

package cn.bluemobi.dylan;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.widget.TextView;

/**
 * 自定义加载进度对话框
 * Created by Dylan on 2016-10-28.
 */

public class LoadingDialog extends Dialog {
    private TextView tv_text;

    public LoadingDialog(Context context) {
        super(context);
        /**设置对话框背景透明*/
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setContentView(R.layout.loading);
        tv_text = (TextView) findViewById(R.id.tv_text);
        setCanceledOnTouchOutside(false);
    }

    /**
     * 为加载进度个对话框设置不同的提示消息
     *
     * @param message 给用户展示的提示信息
     * @return build模式设计,可以链式调用
     */
    public LoadingDialog setMessage(String message) {
        tv_text.setText(message);
        return this;
    }
}

6.用法:一句代码搞定


   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       new LoadingDialog(this).setMessage("正在加载...").show();

   }

7.再次看效果

这里写图片描述

8.GitHub地址https://github.com/linglongxin24/LoadingDialog

上一篇下一篇

猜你喜欢

热点阅读