美文网首页安卓开发面试题
自定义Dialog实现透明无遮罩进度框

自定义Dialog实现透明无遮罩进度框

作者: 手指乐 | 来源:发表于2019-08-22 11:46 被阅读0次

    效果图:


    1. 自定义Dialog继承自Dialog
    public class ProgressDialog extends Dialog {
    
        ImageView ivloading;
        
        public ProgressDialog(Context context) {
            this(context,R.style.Theme_dialog);
        }
        
        public ProgressDialog(Context context, int style) {
            super(context, style);
    
            //set content
            View view = getLayoutInflater().inflate(R.layout.dlg_progress,null);
            setContentView(view);
            ivloading = view.findViewById(R.id.ivloading);
            ((AnimationDrawable)ivloading.getDrawable()).start();
            
            //set window params
            Window window = getWindow();
            WindowManager.LayoutParams params = window.getAttributes();
            params.gravity = Gravity.CENTER;
            params.dimAmount = 0;
            window.setAttributes(params);
        } 
    }
    

    params.dimAmount=0:设置dialog弹出后,背景不出现遮罩,默认会有遮罩

    1. 布局文件:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/ivloading"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:gravity="center"
            android:layout_gravity="center"
            android:src="@drawable/anim_load"
            />
    
    </LinearLayout>
    

    3.Theme_dialog样式:

       <style name="Theme_dialog" parent="@android:style/Theme.Dialog">
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:windowNoTitle">true</item>
        </style>
    

    android:windowBackground设置dialog背景为透明

    4.使用:

    ProgressDialog progressDialog;
    .....
    progressDialog = new ProgressDialog(this);
    ......
    progressDialog.show();
    

    相关文章

      网友评论

        本文标题:自定义Dialog实现透明无遮罩进度框

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