安卓开发面试题

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

2019-08-22  本文已影响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();
上一篇下一篇

猜你喜欢

热点阅读