【问题标题】:How to remove border from Dialog?如何从对话框中删除边框?
【发布时间】:2011-11-08 14:01:51
【问题描述】:

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

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

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

边框消失了,但遗憾的是对话框周围的边距也消失了。

【问题讨论】:

    标签: android


    【解决方案1】:

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

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

    【讨论】:

    • 优雅的解决方案。我将可绘制资源设置为我创建的自定义可绘制资源,而不是透明的。谢谢
    • 谢谢。很好的解决方案。
    • 不错的解决方案,如果不必在多个类中使用它。
    • 你拯救了我的一天兄弟..谢谢:)
    • 非常好它的工作..非常感谢你我最近两天正在寻找这个
    【解决方案2】:

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

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

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

    那么需要的样式是:

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

    【讨论】:

    • 非常感谢!我不知道背景图片也定义了边距。
    • plz,你能把这张 9-patch 的图像发给我吗?我试着修改它,但没有希望,当我删除黑线时,我也失去了阴影效果。
    • 9-patch 图片来自 Android 资源。 9-patch drawable 中的黑线是有原因的,阅读更多关于 9-patch 图像here。
    • @Tomik:对于 ProgressDialog,这会将图像设置在对话框下方,而不是对话框本身。特别是,边界仍然存在。
    【解决方案3】:

    我尝试了一些其他可能性,但使用了具有固定边距的 9 补丁,并发现 layer-list drawable 允许定义偏移量,因此其封闭的可绘制对象周围的边距,所以这对我有用:

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

    【讨论】:

      【解决方案4】:

      另一种选择

      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);            
      

      这些陈述摘自以下sn-p:

      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");
      }
      

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-09-22
          • 1970-01-01
          • 1970-01-01
          • 2014-06-15
          • 1970-01-01
          • 2012-11-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多