zoukankan      html  css  js  c++  java
  • 加载旋转框(loading spinner)

    目标是这样的

    用到的组件 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);
        }
    }
    
  • 相关阅读:
    2015第18周日
    CreateProcess的使用方法
    A ResourcePool could not acquire a resource from its primary factory or source
    ThreadPool.QueueUserWorkItem的性能问题
    Cucumber 入门一
    菜鸟版JAVA设计模式-从抽象与实现说桥接模式
    ServiceStack.Hello——跨平台.net REST api服务搭建
    android看不见main函数怎么办?程序异常了,能够不提示“xxx软件停止执行”吗?
    深入探讨this指针
    问卷星调查学生对《算法》教学的建议与反馈
  • 原文地址:https://www.cnblogs.com/seliote/p/9726634.html
Copyright © 2011-2022 走看看