具有圆角和透明背景的Android自定义警报对话框

Bha*_* Mg 2 transparency android rounded-corners android-alertdialog

我创建了一个自定义AlertDialog使用圆角onDrawLinearLayout下面,

public class RoundedLinearLayout extends LinearLayout {

private Paint drawPaint;
private Paint roundPaint;

private int mCornerRadius = 100;

private RectF bounds;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public RoundedLinearLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    onInit();
}

public RoundedLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    onInit();
}

public RoundedLinearLayout(Context context) {
    super(context);
    onInit();
}

protected void onInit() {
    drawPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    drawPaint.setColor(0xffffffff);
    drawPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    roundPaint.setColor(0xffffffff);

    setWillNotDraw(false);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    if (w != oldw && h != oldh) {
        bounds = new RectF(0, 0, w, h);
    }
}

@Override
protected void dispatchDraw(Canvas canvas) {
    Bitmap bitmap = Bitmap.createBitmap((int) bounds.width(), (int) bounds.height(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bitmap);
    super.dispatchDraw(c);

    BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);

    canvas.drawRoundRect(bounds, mCornerRadius, mCornerRadius, paint);
}
}
Run Code Online (Sandbox Code Playgroud)

然后我添加了透明度getWindow()和设置window.alpha = 0.5f.生成的对话框是,

自定义警报透明度

我想删除那些角落的白色背景.我在这里搜索了100个问题,没有答案可以让我得到完美的圆角警报对话框.任何帮助,将不胜感激!

小智 9

我用它,它对我有用:

ConfirmacionMensaje customDialog = new ConfirmacionMensaje(MainActivity.this);
customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
customDialog.show();
Run Code Online (Sandbox Code Playgroud)

ConfirmacionMensaje来自Dialog

这是我对话框的xml:

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="#ffDB0000"/>
<corners
    android:bottomLeftRadius="4dp"
    android:bottomRightRadius="4dp"
    android:topLeftRadius="4dp"
    android:topRightRadius="4dp" />
</shape>
Run Code Online (Sandbox Code Playgroud)


小智 6

使用警报对话框使用简单的对话框

 LayoutInflater  factory = LayoutInflater.from(getActivity());
            AlertDialog alert = new AlertDialog.Builder(getActivity());

        Dialog  dialog = new Dialog(getActivity());

            dialog.setContentView(your layout);

            dialog.getWindow().setBackgroundDrawable(
                    new ColorDrawable(android.graphics.Color.TRANSPARENT));
Run Code Online (Sandbox Code Playgroud)


Uma*_*hur 5

用这个 :

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Run Code Online (Sandbox Code Playgroud)

这是最简单的解决方案,并且有效。