如何从对话框中移除边框?

21

我有一个在对话框中显示的活动:

为了去掉边框和圆角,我尝试过以下方法:

<resources>
  <style name="ActivityDialog" parent="@android:style/Theme.Dialog">
  <item name="android:windowBackground">@null</item> 
    <item name="android:windowFrame">@null</item>
</style>

边框消失了,但是可悲的是对话框周围的外边距也没有了。

5个回答

71

不需要创建自定义背景图并添加特殊样式,只需在您的代码中添加一行即可:

dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

37

android:windowBackground定义了对话框的边框、圆角和外边距。(在Theme.Dialog样式中,参数android:windowFrame已设置为@null,因此再次将其设置为@null没有效果。)

要去掉边框和圆角,您需要适当更改android:windowBackgroundTheme.Dialog样式将其设置为@android:drawable/panel_background,这是一个9-patch可绘制对象,看起来像这样(此示例为hdpi版本):

enter image description here

正如您所看到的,9-patch png 定义了对话框主题的外边距,边框和圆角。要去掉边框和圆角,您必须创建一个合适的可绘制对象。如果您想保留阴影渐变,您必须创建一组新的9-patch可绘制对象(每个dpi一个可绘制对象)。如果您不需要阴影渐变,可以创建一个形状可绘制对象

所需的样式为:

<style name="ActivityDialog" parent="@android:style/Theme.Dialog">      
   <item name="android:windowBackground">@drawable/my_custom_dialog_background</item>            
</style>

非常感谢!我之前不知道背景图片也会定义边距。 - timoschloesser
请问,你能否发送给我这个9-patch图片?我试图修改它,但是没有成功。当我删除黑线时,也失去了阴影效果。 - K_Anas
9-patch 图像源自 Android。9-patch drawable 中的黑线有其特殊含义,请在此处阅读更多关于 9-patch 图像的信息:http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch - Tomik
1
@Tomik:对于ProgressDialog,这将设置对话框下方的图像,而不是对话框本身。特别是,边框仍然存在。 - Luis A. Florit

2

我尝试了一些其他可能性,但使用带有固定边距的9-patch图案,并发现layer-list可绘制允许定义偏移量,因此可以在其封闭的可绘制对象周围设置边距,所以这对我起作用:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:drawable="@drawable/my_custom_background"
        android:top="5dp" android:bottom="5dp" android:left="5dp" android:right="5dp">
    </item>

</layer-list>

然后您可以将其用作“android:windowBackground”:

<style name="ActivityDialog" parent="@android:style/Theme.Dialog">      
   <item name="android:windowBackground">@drawable/my_custom_layer_background</item>            
</style>

0

另一个选项

Resources\Values\styles.xml

<style name="MessageDialog" parent="android:Theme.Holo.Light.Dialog">
   <item name="android:windowBackground">@android:color/transparent</item>
</style>

在哪里

AlertDialog.Builder builder = new AlertDialog.Builder(Activity, Resource.Style.MessageDialog);            

这些语句摘自以下代码片段:

public class MessageAlertDialog : DialogFragment, IDialogInterfaceOnClickListener
{
    private const string DIALOG_TITLE = "dialogTitle";
    private const string MESSAGE_TEXT = "messageText";
    private const string MESSAGE_RESOURCE_ID = "messageResourceId";

    private string _dialogTitle;
    private string _messageText;
    private int _messageResourceId;

    public EventHandler OkClickEventHandler { get; set; }

    public static MessageAlertDialog NewInstance(string messageText)
    {
        MessageAlertDialog dialogFragment = new MessageAlertDialog();

        Bundle args = new Bundle();
        args.PutString(MESSAGE_TEXT, messageText);

        dialogFragment.Arguments = args;

        return dialogFragment;
    }

    public static MessageAlertDialog NewInstance(string dialogTitle, string messageText)
    {
        MessageAlertDialog dialogFragment = new MessageAlertDialog();

        Bundle args = new Bundle();
        args.PutString(DIALOG_TITLE, dialogTitle);
        args.PutString(MESSAGE_TEXT, messageText);

        dialogFragment.Arguments = args;

        return dialogFragment;
    }

    public static MessageAlertDialog NewInstance(int messageResourceId)
    {
        MessageAlertDialog dialogFragment = new MessageAlertDialog();

        Bundle args = new Bundle();
        args.PutInt(MESSAGE_RESOURCE_ID, messageResourceId);

        dialogFragment.Arguments = args;

        return dialogFragment;
    }        

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        _dialogTitle = Arguments.GetString(DIALOG_TITLE);
        _messageText = Arguments.GetString(MESSAGE_TEXT);
        _messageResourceId = Arguments.GetInt(MESSAGE_RESOURCE_ID);
    } 

    public override Dialog OnCreateDialog(Bundle savedInstanceState)
    {
        base.OnCreateDialog(savedInstanceState);

        AlertDialog.Builder builder = new AlertDialog.Builder(Activity, Resource.Style.MessageDialog);            

        if (_dialogTitle != null)
        {
            builder.SetTitle(_dialogTitle);
        }

        if (_messageText != null)
        {
            builder.SetMessage(_messageText);
        }
        else
        {
            View messageView = GetMessageView();
            if (messageView != null)
            {
                builder.SetView(messageView);
            }
        }

        builder.SetPositiveButton("OK", this);
               //.SetCancelable(false);            

        this.Cancelable = false;
        AlertDialog dialog = builder.Create();
        dialog.SetCanceledOnTouchOutside(false);
        //dialog.Window.SetBackgroundDrawableResource(Android.Resource.Color.Transparent);

        return dialog;            
    } 

    private View GetMessageView()
    {
        if (_messageResourceId != 0)
        {
            View messageView = Activity.LayoutInflater.Inflate(_messageResourceId, null);

            return messageView;
        }

        return null;
    }

    void IDialogInterfaceOnClickListener.OnClick(IDialogInterface di, int i)
    {
        OkClickEventHandler?.Invoke(this, null);
    }
}

使用方法

public static void ShowMessageAlertDialog(FragmentManager fragmentManager, string dialogTitle, string messageText, EventHandler okClickEventHandler)
{
    MessageAlertDialog msgAlertDialog = MessageAlertDialog.NewInstance(dialogTitle, messageText);
    msgAlertDialog.OkClickEventHandler += okClickEventHandler;

    msgAlertDialog.Show(fragmentManager, "message_alert_dialog");
}

0

解决这个问题的另一个方法是使用android.support.v7.app.AlerDialog而不是android.app.AlertDialog。这是最简单和最有效的解决方案。在布局中设计您自定义的视图,然后使用support包的AlertDialogBuilder类与之配合,它会像魔法一样工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接