如何在android中的简单Alert对话框中添加左右边距? (比方说两边屏幕宽度的15%)

时间:2016-08-18 10:57:35

标签: android webview margin alertdialog android-alertdialog

我想在左侧和右侧边距添加Android警报对话框,让我们说15%的屏幕宽度,这是我的代码到目前为止,我该怎么做?

AlertDialog.Builder alert = new AlertDialog.Builder(context);

WebView wv = new WebView(context);
wv.setBackgroundColor(getResources().getColor(android.R.color.transparent));
wv.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
});
wv.loadUrl(ANY_URL_STRING);

alert.setView(wv);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        dialog.dismiss();
    }
});
alert.show();

1 个答案:

答案 0 :(得分:0)

为您的aleart对话框创建自定义布局,并在xml中提供边距。

LayoutInflater li = LayoutInflater.from(class.this);
                View promptsView = li.inflate(R.layout.custom_layout, null);
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        class.this);
                alertDialogBuilder.setView(promptsView);

                final WebView wv= (WebView) promptsView.findViewById(R.id.webview);

                // create alert dialog
                alertDialog = alertDialogBuilder.create();
                // show it
                alertDialog.show();
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int id) {
        dialog.dismiss();
    }
});