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

Ste*_*eve 30 android android-layout android-dialog

之前已经问过这个问题:AlertDialog自定义标题有黑色边框

但没有得到满意的答复 - 并且缺少一些信息.


我正在尝试在没有标题的情况下在Android中创建自定义对话框,并且底部没有任何按钮.

但是,生成的对话框在视图的顶部和底部有黑色"边框"/"间距"/某些内容.

文档:

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

那就是我做的:

Welcome.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();
    }
}
Run Code Online (Sandbox Code Playgroud)

welcomedialog.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>
Run Code Online (Sandbox Code Playgroud)

注:我已经尝试使用FrameLayout为根ViewGroup,而不是LinearLayout为每一个建议,我发现某处-但这并没有帮助.

结果

在此输入图像描述 在此输入图像描述


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();
    }
}
Run Code Online (Sandbox Code Playgroud)

不适合我.

Ste*_*eve 39

如果你看一下AlertDialog类源,你会发现大多数方法都只是代理方法(facade)private AlertController mAlert.

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

private int mViewSpacingLeft;
private int mViewSpacingTop;
private int mViewSpacingRight;
private int mViewSpacingBottom;
private boolean mViewSpacingSpecified = false;
Run Code Online (Sandbox Code Playgroud)

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

通过更改此行可以正确完成此操作:

dialog.setView(layout);
Run Code Online (Sandbox Code Playgroud)

至:

dialog.setView(layout, 0, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)

  • 通过这个小编辑为我工作,谢谢!`AlertDialog dialog = builder.create();``dialog.setView(layout,0,0,0,0);``dialog.show();` (5认同)

Sha*_*wal 8

dialog.setInverseBackgroundForced(true);
Run Code Online (Sandbox Code Playgroud)

在代码中使用上面的内容来删除警告对话框的边框.

请参阅此LINK以获取InverseBackgroundForced.

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

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 这将黑色变为白色 - 但仍留有空间. (5认同)
  • False使背景变黑 - 但仍然留下空间. (3认同)

小智 6

在我的例子中,该边框是由AlertDialog的父Activity的主题引起的.要完全摆脱边框,请给它一个不同的主题(在本例中为Holo):

AlertDialog.Builder builder = new AlertDialog.Builder(
                                new ContextThemeWrapper(this, android.R.style.Theme_Holo)
                              );
Run Code Online (Sandbox Code Playgroud)

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