我可以动态更改整个android.support.v7.app.AlertDialog的背景吗?

时间:2019-01-07 16:26:19

标签: android android-dialog android-background

我为此背景生成了一个特殊的位图,现在我需要将其设置为android.support.v7.app.AlertDialog,但是我找不到动态的方法。

我试图为其创建一个子类,但没有像onWindowFocusChanged这样的操作在调试中触发-因为我想访问视图的大小并为其设置背景

public class CustomDialog extends AlertDialog {
    protected CustomDialog(@NonNull Context context) {
        super(context);
    }

    protected CustomDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected CustomDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onStart() {
        super.onStart();
        int width = getWindow().getDecorView().getWidth();
        int height = getWindow().getDecorView().getHeight();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
//It doesn't trigger here
    }

另外,这是我初始化的方式:

 public static void showDialog(Context context, String title, String message, final Runnable runnable){
        new CustomDialog.Builder(context)
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton(context.getString(android.R.string.ok), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if (runnable != null) runnable.run();
                        dialog.dismiss();
                    }
                })
                .setNegativeButton(context.getString(android.R.string.cancel), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                })
                .show();
    }

0 个答案:

没有答案