Android 背景透明的自定alertDialog

话不多说先上图

Android 背景透明的自定alertDialog

alertDialog 背景是透明的

public class AppProgressBar extends Dialog {

    private String hintStr;
    private TextView hintText;//提醒文本

    public AppProgressBar(Context context, String hintStr) {
        super(context, R.style.DialogBackgroundNull);
        this.hintStr = hintStr;//提醒文本,我就是在提醒文本里,设置的“登陆中...”
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.app_progress);

        Window window = getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();

        window.setLayout(DenityUtil.dipToPx(130),
                WindowManager.LayoutParams.WRAP_CONTENT);

//第一个参数是宽度,我本来想用WindowManager.LayoutParams.WRAP_CONTENT来着,发现根本不管用 233

//因为app是全屏的所以调用了一下全屏,但是适配好像有点问题,在我手机上可以,在俺娘手机就不行了,如果哪位老铁有好//一点的欢迎告诉我啦
        fullScreen();

        layoutParams.x = 0;
        layoutParams.y =  - DenityUtil.dipToPx(20);

        hintText = findViewById(R.id.hintText);
        hintText.setText(hintStr);
        setCancelable(false);//不能取消
      /*  ImageView  image = findViewById(R.id.image);
        Glide.with(getContext()).load(R.raw.back_loading).asGif().into(image);*/
    }

    /**
     * 全屏
     */
    private  void fullScreen(){
        if ( Build.VERSION.SDK_INT >= 19) {
            View decorView = getWindow().getDecorView();
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
        }
        else {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(option);
        }
    }

}

最重要的是这个style文件,背景透明关键

<style name="DialogBackgroundNull" parent="@android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item> <!--无边框-->
    <item name="android:windowIsFloating">true</item><!--悬浮在父界面之上-->
    <item name="android:windowIsTranslucent">true</item><!--允许呈现半透明状态-->
    <item name="android:windowNoTitle">true</item><!--无标题-->
    <item name="android:windowBackground">@color/trans_color</item> <!--透明背景-->
    <item name="android:backgroundDimEnabled">true</item> <!-- 允许周围模糊 -->
    <item name="android:backgroundDimAmount">0.6</item><!--设置模糊灰度-->
</style>

布局文件

<LinearLayout android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout
      android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        >
 <!--播放Gif的View,从网上找的,这个其实还挺多的-->
        <com.example.mobileproject.custom_view.GifView
            android:layout_centerInParent="true"
            android:layout_width="110dp"
            android:layout_height="110dp"
            app:gif="@raw/back_loading"/>
 

<!--其实就是那个蓝色边框-->
        <ImageView
            android:layout_width="111dp"
            android:layout_height="111dp"
            android:src="@drawable/white_circle"
            />

    </RelativeLayout>

    <!--提示文本-->
    <TextView
        android:id="@+id/hintText"
        android:layout_marginTop="10dp"
        android:textColor="#fff"
        android:textSize="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

如果觉得写的好的话,记得给我留言哦,嘿嘿