【问题标题】:How to add left and right margins on a simple Alert dialog in android? (lets say 15 percent of the screen width on both sides) [duplicate]如何在android中的简单警报对话框上添加左右边距? (让我们说两边屏幕宽度的 15%)[重复]
【发布时间】:2016-08-18 19:25:45
【问题描述】:

我想在两侧的 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();

【问题讨论】:

  • 创建自定义对话框并在 XML 中添加您想要的内容

标签: android webview margin android-alertdialog


【解决方案1】:

为您的 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();
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 2011-10-26
    • 1970-01-01
    • 2012-02-26
    • 2018-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多