加载旋转框(loading spinner)

目标是这样的
1267954-20180929201255520-578742088.jpg

用到的组件 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();
        }
    }
}

效果
1267954-20180929204626706-962027588.gif
背景在弹窗后自动变暗,不怎么好看,修改一下

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)
效果
1267954-20180929205359244-516320564.gif

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);
    }
}

转载于:https://www.cnblogs.com/seliote/p/9726634.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值