如何从Dialog中删除边框?

时间:2011-11-08 14:01:52

标签: android

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

为了删除边框和圆角,我尝试了这个:

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

边界已经消失,但遗憾的是对话框周围的边缘。

5 个答案:

答案 0 :(得分:72)

如果不创建自定义背景可绘制并添加特殊样式,只需在代码中添加一行:

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

答案 1 :(得分:37)

边框,圆角和边距均由android:windowBackground定义。 (参数android:windowFrame已设置为@null样式中的Theme.Dialog,因此再次将其设置为@null无效。)

要删除边框和圆角,您必须适当更改android:windowBackgroundTheme.Dialog样式将其设置为@android:drawable/panel_background。这是一个9-patch drawable,看起来像这样(这个是hdpi版本):

enter image description here

正如您所看到的,9-patch png定义了对话框主题的边距,边框和圆角。要删除边框和圆角,您必须创建适当的drawable。如果你想保持阴影渐变,你必须创建一组新的9-patch drawables(每个dpi一个drawable)。如果您不需要阴影渐变,则可以创建shape drawable

所需的风格是:

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

答案 2 :(得分:2)

我玩了一些其他的可能性,但使用了一个固定边距的9补丁,发现图层列表drawable允许定义偏移量,因此围绕其封闭的drawables边缘,所以这对我有用:

<?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>

答案 3 :(得分:0)

另一个选择

<强>资源\值\ 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");
}

答案 4 :(得分:0)

解决此问题的另一种方法是使用android.support.v7.app.AlerDialog而不是android.app.AlertDialog。这是最简单,最省时的解决方案。在布局中设计您的自定义视图,然后将其与support包的AlertDialogBuilder类一起使用,它将像魅力一样工作。

相关问题