美文网首页
Android 自定义Loading

Android 自定义Loading

作者: 蓝色_fea0 | 来源:发表于2019-11-14 18:16 被阅读0次

    1.创建一个layout

    dialog_loadding.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">
    
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:background="@null">
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="0dp"
                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/shape_progress_bar" />
    
    
                <TextView
                    android:id="@+id/tv_text"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:layout_marginBottom="16dp"
                    android:text="正在加载"
                    android:textColor="#fff" />
            </LinearLayout>
    
        </RelativeLayout>
    
    </RelativeLayout>
    

    2创建LoadingDialog

    package com.companyname.view;
    
    import android.app.Dialog;
    import android.content.Context;
    import android.graphics.Color;
    import android.graphics.drawable.ColorDrawable;
    import android.support.annotation.NonNull;
    import android.view.MotionEvent;
    import android.widget.TextView;
    
    import com.companyname.R;
    
    public class LoadingDialog extends Dialog {
    
        private TextView tv_text;
    
        public LoadingDialog(Context context) {
            super(context);
            //设置对话框背景透明
            if (getWindow()!= null) {
                getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                setContentView(R.layout.dialog_loadding);
                tv_text = findViewById(R.id.tv_text);
                setCanceledOnTouchOutside(false);
            }
        }
    
        /**
         * 为加载进度个对话框设置不同的提示消息
         *
         * @param message 给用户展示的提示信息
         * @return build模式设计,可以链式调用
         */
        public LoadingDialog setMessage(String message) {
            tv_text.setText(message);
            return this;
        }
    
        @Override
        public boolean onTouchEvent(@NonNull MotionEvent event) {
            return true;
        }
    
        @Override
        public void onBackPressed() {
    
        }
    }
    

    在代码中使用

        private void initDialog() {
            if (loadingDialog == null) {
                loadingDialog = new LoadingDialog(mContext);
            }
        }
    
        private void showDialog() {
            if (loadingDialog == null) {
                initDialog();
            }
    
            if (loadingDialog.isShowing()) {
                loadingDialog.hide();
            }
    
            loadingDialog.show();
    
        }
    
        private void hideDialog() {
            if (loadingDialog == null) {
                initDialog();
            }
    
            if (loadingDialog.isShowing()) {
                loadingDialog.hide();
            }
        }
    

    相关文章

      网友评论

          本文标题:Android 自定义Loading

          本文链接:https://www.haomeiwen.com/subject/mxfdictx.html