How to remove a border from a dialog box?

I have an activity that is shown in a dialog box:

To remove borders and rounded corners, I tried this:

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

The border has disappeared, but unfortunately also the margin around the dialogue.

+20
android
Nov 08 2018-11-11T00:
source share
4 answers

The border, round corners and margin are determined by android:windowBackground . (The android:windowFrame already set to @null in @null style, so setting it to @null does not affect it again.)

To remove borders and round corners, you must modify android:windowBackground . Theme.Dialog style sets the value of @android:drawable/panel_background . What is a 9-patch that looks like this (this is the hdpi version):

enter image description here

As you can see, the 9-patch png defines the borders, borders and round corners of the dialogue theme. To remove a border and round corners, you must create a suitable option. If you want to keep the shadow gradient, you need to create a set of new 9-patch drawings (one for each dpi). If you do not need a shadow gradient, you can create a shaped shape .

Required Style:

 <style name="ActivityDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowBackground">@drawable/my_custom_dialog_background</item> </style> 
+36
Nov 08 '11 at 16:24
source share

Without creating a custom background and adding a special style, just add one line to the code:

 dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); 
+66
Feb 12 '13 at 6:19 06:19
source share

I played around a bit with other features, but used 9 patches with fixed fields and found out that draw-list-layer allows you to define offsets, hence the fields around its closed drawings, so this worked for me:

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

and then you can use this as "android: windowBackground":

 <style name="ActivityDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowBackground">@drawable/my_custom_layer_background</item> </style> 
+1
Dec 20 '11 at 16:35
source share

Another variant

Resources \ Values ​​\ styles.xml

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

Where

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

These statements are written from the following passage:

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

Using

 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
Jul 26 '17 at 2:56
source share



All Articles