目标是这样的

用到的组件 AlertDialogProgressBar

先创建一个 AlertDialog 的布局

<?xml version="1.0" encoding="utf-8"?>

<!-- File: res/layout/dialog_loading.xml -->
<ProgressBar
style="?android:progressBarStyleLarge"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

接下来创建弹窗类,需要将背景设置为透明,否则背景很难看

package com.seliote.loadingdialog;

/**
* File: com/seliote/loadingdialog/LoadingDialog.java
*/ import android.content.Context;
import android.support.v7.app.AlertDialog; public class LoadingDialog {
private Context mContext;
private AlertDialog mAlertDialog; public LoadingDialog(Context aContext) {
mContext = aContext;
mAlertDialog = new AlertDialog.Builder(mContext)
.setView(R.layout.dialog_loading)
.create();
// Set AlertDialog background to transparent
mAlertDialog.getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
} public void show() {
if (!mAlertDialog.isShowing()) {
mAlertDialog.show();
}
} public void dismiss() {
if (mAlertDialog.isShowing()) {
mAlertDialog.dismiss();
}
}
}

效果



背景在弹窗后自动变暗,不怎么好看,修改一下

res/layout/styles.xml 中添加 style 标签

<!-- Disable background dim for AlertDialog -->
<style name="NoDimAlertDialog" parent="Theme.AppCompat.Dialog.Alert">
<item name="android:backgroundDimEnabled">false</item>
</style>

应用标签,LoadingDialog.java 中创建 AlertDialog 时使用自定义样式

mAlertDialog = new AlertDialog.Builder(mContext, R.style.NoDimAlertDialog)

效果

ok,大体完成

再来点微调,下方加上提示符,loading_dialog.xml 改为

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <!-- style="?android:progressBarStyleLarge" 变成大圈 -->
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/> <!-- 颜色默认为白色,记得改一下,不然看不到 -->
<TextView
android:id="@+id/loading_dialog_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="16dp"
android:text="@string/default_loading_dialog_text"
android:textSize="16sp"
android:textAllCaps="false"
android:textColor="@android:color/darker_gray"/> </LinearLayout>

LoadingDialog.java 改为

package com.seliote.driftbottle.component;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView; import com.seliote.driftbottle.R; /**
* 加载旋转框,用于耗时操作时堵塞用户操作
*/
public class LoadingDialog { private Context mContext;
private AlertDialog mAlertDialog;
private View mView; /**
* 构造一个可以阻塞用户操作的的弹窗对象
*
* @param aContext 上下文对象
*/
public LoadingDialog(Context aContext) {
this.mContext = aContext; this.mView = LayoutInflater.from(aContext).inflate(R.layout.dialog_loading, null); this.mAlertDialog = new AlertDialog
.Builder(this.mContext, R.style.NoDimAlertDialog)
.setView(this.mView)
.setCancelable(false)
.create();
// 将背景设置为透明的
this.mAlertDialog.getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
} /**
* 构造一个可以阻塞用户操作的的弹窗对象,并指定提示字符
*
* @param aContext 上下文对象
* @param aPromptText 提示字符
*/
public LoadingDialog(Context aContext, @NonNull String aPromptText) {
this.mContext = aContext; this.mView = LayoutInflater.from(aContext).inflate(R.layout.dialog_loading, null); TextView textView = this.mView.findViewById(R.id.loading_dialog_text_view);
textView.setText(aPromptText); this.mAlertDialog = new AlertDialog
.Builder(this.mContext, R.style.NoDimAlertDialog)
.setView(this.mView)
.setCancelable(false)
.create();
// 将背景设置为透明的
this.mAlertDialog.getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
} /**
* 显示弹窗
*/
public void show() {
if (!this.mAlertDialog.isShowing()) {
this.mAlertDialog.show();
}
} /**
* 隐藏弹窗
*/
public void dismiss() {
if (this.mAlertDialog.isShowing()) {
this.mAlertDialog.dismiss();
}
} /**
* 更改提示字符
*
* @param aPrompt 提示字符
*/
public void changePrompt(String aPrompt) {
TextView textView = this.mView.findViewById(R.id.loading_dialog_text_view);
textView.setText(aPrompt);
}
}

加载旋转框(loading spinner)的更多相关文章

  1. js spin 加载动画(loading)

    js spin 加载动画 最近做页面ajax加载是又用到loading动画,还好有一个spin.js 具体的包大家可以去http://fgnass.github.com/spin.js/下载, 如果想 ...

  2. IOS开发UI篇之──自定义加载等待框(MBProgressHUD)

    本文转载至 http://blog.csdn.net/xunyn/article/details/8064984   原文地址http://www.189works.com/article-89289 ...

  3. js图片未加载完显示loading效果

    <html> <title>js图片未加载完显示loading效果</title> <body> <style> img{float:lef ...

  4. flutter的加载弹框

    代码组件: import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import 'packa ...

  5. 安卓 自定义AlertDialog对话框(加载提示框)

    AlertDialog有以下六种使用方法: 一.简单的AlertDialog(只显示一段简单的信息) 二.带按钮的AlertDialog(显示提示信息,让用户操作) 三.类似ListView的Aler ...

  6. JS实现页面加载完毕之前loading提示效果

    1.获取浏览器页面可见高度和宽度 var _PageHeight = document.documentElement.clientHeight, _PageWidth = document.docu ...

  7. js实现的页面加载完毕之前loading提示效果

    页面加载readyState的五种状态 原文如下: 0: (Uninitialized) the send( ) method has not yet been invoked. 1: (Loadin ...

  8. 加载页面(Loading)

    /* 文件说明:页面加载时Loading JS 文件描述:解决IE或FF下,初始化加载时,页面布局乱掉的问题,参考:*/var width = $(window).width();var height ...

  9. appcloud 加载第三方页面loading效果

    apiready = function() { var header = $api.byId('header'); $api.fixIos7Bar(header); var headerPos = $ ...

随机推荐

  1. bind 详解

    请看我的有道云笔记: http://note.youdao.com/noteshare?id=eaf4194473cf4294776fbc263ffe6b89&sub=5CB214C594E0 ...

  2. 如何删除Windows 10中的内存转储文件

    内存转储文件是由Windows产生的.以下情况下可能产生内存转储文件: 计算机崩溃蓝屏 内存错误 硬件问题 内存转储文件包含计算机系统崩溃时的详细的参数副本.用于帮助识别导致系统崩溃的原因.Windo ...

  3. 在浏览器里使用SAPGUI里的SE80

    效果如图:点击Fiori launchpad的SE80对应的tile: 即可在浏览器里打开SE80 具体步骤 (1). 在后台找到Fiori catalog page ID: SAP_FIORI_EX ...

  4. 我是一只IT小小鸟读后感 Part 1

    我是一只IT小小鸟读后感 Part 1 梦断计院 作为一个工科生,真的和作者想到一块去了.在科大这个环境下,GPA成了衡量一个学生优秀与否的唯一因素,而真正对于编程和技术性的东西有兴趣的,往往被埋没在 ...

  5. 最新DNS汇集

    最近几日DNS大规模抽风,网络环境是一天比一天恶劣,于是收集了一些良心的DNS服务器地址,以备不时之需. 国内服务器: 1.OpenerDNS:42.120.21.30 2.114DNS:114.11 ...

  6. (第四场)G Maximum Mode 【YY+暴力】

    链接:https://www.nowcoder.com/acm/contest/142/G 来源:牛客网 题目描述 The mode of an integer sequence is the val ...

  7. 整个简历的讲解(falling+mimic+refidet)

    1.解决方案 下边缘: a.论文的数据来自kitti,gt数据来自于激光雷达,利用kitti自带的开发包先将激光雷达的数据映射到图片的二维平面,每个x,y会生成对应的d(x,y),即depth.再对每 ...

  8. 在vue项目中的axios使用配置记录

    默认vue项目中已经安装axios,基于element-ui开发,主要记录配置的相关. axiosConfig.js import Vue from 'vue' import axios from ' ...

  9. qbxt Day4

    1.树形dp 例题1 树上最长链 其实有两种方法,但为了简便,就只学了最通用的dp算法 我们考虑设dp[i][0/1]表示以i为根的最长路和次长路,然后拼接就行了 第二维0表示最长路,1表示次长路 i ...

  10. Shiro 登录认证源码详解

    Shiro 登录认证源码详解 Apache Shiro 是一个强大且灵活的 Java 开源安全框架,拥有登录认证.授权管理.企业级会话管理和加密等功能,相比 Spring Security 来说要更加 ...