簡體   English   中英

具有圓角和透明背景的Android自定義警報對話框

[英]Android custom alert dialog with rounded corners and transparent background

我使用onDraw of LinearLayout創建了一個帶圓角的自定義AlertDialog,如下所示,

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 生成的對話框是,

自定義警報透明度

我想刪除那些角落的白色背景。 我在這里搜索了100個問題,沒有答案可以讓我得到完美的圓角警報對話框。 任何幫助,將不勝感激!

我用它,它對我有用:

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

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>

使用警報對話框使用簡單的對話框

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

用這個 :

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

這是最簡單的解決方案,它的工作原理。

這對我有用

dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.background_verification));

后台驗證是我的可繪制文件

這可以解決:

   dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.background_verification));

你確定要使用對話框嗎? 它似乎更像是一個臨時彈出窗口,如吐司或烤面包片:

關於背景,你可以使用一個9補丁或自定義xml drawable( 這里這里的例子)...

如果對話框是AlertDialogDialog的實例,請在代碼中添加以下內容:

myDialog
    .getWindow()
    .setBackgroundDrawable(new ColorDrawable(Color.argb(0,0,0,0)));

附注:擴展LinearLayout以應用圓形框,在我看來這不是一個好習慣,您可以通過非常簡單的XML表示來做到這一點,在這種情況下,XML矩形形狀可以幫助更多:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    ...
    <corners
        android:radius="3dp" />
    ...
</shape>

這對我來說是第一次

dialog.getWindow().setBackgroundDrawable(getResources().getDrawable(R.drawable.background_verification));

這里我從drawable文件夾獲取資源,background_verification是可繪制文件

  1. 使用dialog_corner在drawable文件夾中創建xml。

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@color/main_background"/> <corners android:topLeftRadius="@dimen/margin_10" android:topRightRadius="@dimen/margin_10" android:bottomRightRadius="@dimen/margin_10" android:bottomLeftRadius="@dimen/margin_10" /> </shape>

2.布局輸出

機器人:背景= “@繪制/ dialog_corner”

3.在你的java文件中保留下面的代碼

View mView =LayoutInflater.from(mContext).inflate(R.layout.layout_pob,null); 
         alertDialog.getWindow().setBackgroundDrawable(new   ColorDrawable(Color.TRANSPARENT));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM