天天看点

圆角Dialog 四角存在默认背景颜色问题解决

布局文件根布局加

android:background="@drawable/rectangle"
           

rectangle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!--填充颜色-->
    <solid android:color="@color/white"></solid>
    <!--线的宽度、颜色-->
    <stroke android:width="2dp" android:color="@color/white"/>
    <!--矩形圆角-->
    <corners android:radius="15dp"/>
</shape>
           

styles.xml

<!-- 自定义loading dialog -->
    <style name="loading_dialog" parent="android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:drawable/screen_background_light</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
    </style>
           

createDialog 时将默认背景改成透明的,不然带着4个角

loadingDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加载view
Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 创建自定义样式dialog
        loadingDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        loadingDialog.setCancelable(false);// 不可以用“返回键”取消
        loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.FILL_PARENT,
                LinearLayout.LayoutParams.FILL_PARENT));// 设置布局
        return loadingDialog;