Android AlertDialog:如何删除视图上方和下方的黑色边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10433764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:43:58  来源:igfitidea点击:

AlertDialog: How To Remove Black Borders Above and Below View

androidandroid-layoutandroid-dialog

提问by Steve

This question has been asked before: AlertDialog custom title has black border

之前有人问过这个问题:AlertDialog custom title has black border

But was not answered satisfactorily - and is missing some information.

但没有得到令人满意的回答 - 并且缺少一些信息。



I'm trying to create a custom dialog in Android without a title and without any buttons along the bottom.

我正在尝试在 Android 中创建一个没有标题且底部没有任何按钮的自定义对话框。

However, the resulting dialog has black "borders"/"spacing"/something along the top and bottom of the view.

但是,生成的对话框在视图的顶部和底部有黑色的“边框”/“间距”/东西。

From the Documentation:

文档

A dialog made with the base Dialog class must have a title. If you don't call setTitle(), then the space used for the title remains empty, but still visible. If you don't want a title at all, then you should create your custom dialog using the AlertDialog class. However, because an AlertDialog is created easiest with the AlertDialog.Builder class, you do not have access to the setContentView(int) method used above. Instead, you must use setView(View). This method accepts a View object, so you need to inflate the layout's root View object from XML.

使用 Dialog 基类创建的对话框必须有一个标题。如果您不调用 setTitle(),则用于标题的空间仍为空,但仍然可见。如果您根本不需要标题,那么您应该使用 AlertDialog 类创建您的自定义对话框。但是,因为使用 AlertDialog.Builder 类最容易创建 AlertDialog,所以您无法访问上面使用的 setContentView(int) 方法。相反,您必须使用 setView(View)。此方法接受一个 View 对象,因此您需要从 XML 扩展布局的根 View 对象。

So, that's what I did:

所以,这就是我所做的:

Welcome.java

欢迎.java

public class Welcome  extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.welcome);

        LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.welcomedialog, (ViewGroup)findViewById(R.id.layout_root));

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(layout);
        builder.create().show();
    }
}

welcomedialog.xml

欢迎对话框.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="@drawable/texturebg"
              android:id="@+id/layout_root"
              android:orientation="vertical"
              android:padding="40px">
    ...
</LinearLayout>

NOTE:I've tried using FrameLayoutas the root ViewGroupinstead of LinearLayoutas per a suggestion I found somewhere - but that didn't help.

注意:我已经尝试使用FrameLayout作为根目录ViewGroup而不是LinearLayout按照我在某处找到的建议 - 但这没有帮助。

Result

结果

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明



setBackgroundDrawable Suggestion

setBackgroundDrawable 建议

public class Welcome  extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.welcome);

        LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.welcomedialog, (ViewGroup)findViewById(R.id.layout_root));

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(layout);
        AlertDialog dialog = builder.create();

        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));

        dialog.show();
    }
}

Didn't work for me.

没有为我工作。

回答by Steve

If you look at the AlertDialog class sourceyou'll see most of the methods are simply proxy methods (facade) around private AlertController mAlert.

如果您查看AlertDialog 类源代码,您会发现大多数方法只是围绕 的代理方法(外观)private AlertController mAlert

Looking at the AlertController class sourceyou'll see 4 interesting member variables:

查看AlertController 类源代码,您将看到 4 个有趣的成员变量:

private int mViewSpacingLeft;
private int mViewSpacingTop;
private int mViewSpacingRight;
private int mViewSpacingBottom;
private boolean mViewSpacingSpecified = false;

Setting mViewSpacingSpecifiedto truewill remove the borders on the top and bottom of the dialog.

设置mViewSpacingSpecifiedtrue将删除对话框顶部和底部的边框。

This is done properly by changing this line:

这是通过更改此行正确完成的:

dialog.setView(layout);

to:

到:

dialog.setView(layout, 0, 0, 0, 0);

回答by Shankar Agarwal

dialog.setInverseBackgroundForced(true);

use the above in your code to remove the border of the alert dialog.

在您的代码中使用上面的内容来删除警报对话框的边框。

Refer this LINKfor InverseBackgroundForced.

请参阅此链接以了解 InverseBackgroundForced。

UPDATEDTry this code::::

更新试试这个代码::::

public class Welcome  extends Activity
{
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.welcome);

        AlertDialog.Builder builder = new AlertDialog.Builder(Welcome.this);
        LayoutInflater _inflater = LayoutInflater.from(Welcome.this);
        View view = _inflater.inflate(R.layout.welcomedialog,null);
        builder.setView(view);

        AlertDialog alert = builder.create();
        alert.show();
    }
}

Note::Also try by removing android:padding="40px" from welcomedialog.xml.

注意::也可以尝试从welcomedialog.xml 中删除android:padding="40px"。

回答by Prismatica

In my case, that border was caused by the theme of the parent Activity for the AlertDialog. To get rid of the border completely, give it a different theme (in this case, Holo):

就我而言,该边框是由 AlertDialog 的父 Activity 的主题引起的。为了完全摆脱边框,给它一个不同的主题(在这种情况下,Holo):

AlertDialog.Builder builder = new AlertDialog.Builder(
                                new ContextThemeWrapper(this, android.R.style.Theme_Holo)
                              );

This fixed it for me. Hope this helps!

这为我修好了。希望这可以帮助!

回答by jsidera

Just to make Steve's answer more clear, this can be done easily. For example in my case the view I was setting in the dialog was a WebView.

只是为了让史蒂夫的回答更清楚,这很容易做到。例如,在我的情况下,我在对话框中设置的视图是 WebView。

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    WebView webView = new WebView(getActivity());
    webView.loadUrl(" url to show ");


    OnClickListener clickListenerOk = new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ...
        }
    };

    OnClickListener clickListenerCancel = new OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            ...
        }
    };

    AlertDialog dialog = new AlertDialog.Builder(getActivity())

            .setPositiveButton("OK", clickListenerOk)

            .setNegativeButton("Cancel",clickListenerCancel)

            .create();

    dialog.setView(webView, 0, 0, 0, 0);

    return dialog;
}

回答by Changwei Yao

setBackgroundDrawable(new ColorDrawable(0));

Call this in your dialog.

在您的对话框中调用它。