带有圆角和透明背景的Andr​​oid定制警报对话框 [英] Android custom alert dialog with rounded corners and transparent background

查看:210
本文介绍了带有圆角和透明背景的Andr​​oid定制警报对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用自定义AlertDialog带有圆角的OnDraw 的LinearLayout 如下,

I have created a custom AlertDialog with rounded corners using onDraw of LinearLayout as below,

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);
}
}

然后我加入的透明度 getWindow()和设置 window.alpha = 0.5F 。 由此产生的对话框,

And then I added transparency by getWindow() and setting window.alpha = 0.5f . The resulting dialog is,

我想删除这些角落白色背景。我已经找遍了数百个问题在这里没有答案可以给我完美的圆角警告对话框。任何帮助将是AP preciated!

I want to remove those corner white background. I have searched 100s of questions here and no answer could get me the perfect rounded corner alert dialog. Any help would be appreciated!

推荐答案

我用这个和它的工作对我来说:

I use this and it worked for me:

ConfirmacionMensaje customDialog = new ConfirmacionMensaje(MainActivity.this);
customDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
customDialog.show();

ConfirmacionMensaje exntends从对话框

ConfirmacionMensaje exntends from Dialog

这是我的xml的对话:

and this is my xml for Dialog:

<?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>

这篇关于带有圆角和透明背景的Andr​​oid定制警报对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆