2011-11-08 59 views
20

我這是在一個對話框中顯示的活動:如何從對話框中刪除邊框?

爲了消除邊框和四角圓潤,我想這:

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

的邊境走了,但不幸的是對話的邊緣。

回答

36

邊框,圓角和邊距均由android:windowBackground定義。 (參數android:windowFrame已被設置爲@nullTheme.Dialog風格,因此將其設置爲@null再沒有任何影響。)

爲了消除你必須適當地改變android:windowBackground邊框和圓角。 Theme.Dialog風格將其設置爲@android:drawable/panel_background。這是9補丁繪製的,看起來像這樣(這個人是華電國際版):

enter image description here

正如你可以看到9補丁PNG定義對話框主題的空白,邊框和圓角。要刪除邊框和圓角,你必須創建一個合適的drawable。如果你想保持陰影漸變,你必須創建一組新的9-patch drawable(每個dpi可繪製一個)。如果你不需要陰影梯度,你可以創建一個shape drawable

,所需式則是:

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

非常感謝!我不知道背景圖片也定義了邊距。 – timoschloesser

+0

plz,你可以給我發送這個9-patch圖像,我嘗試修改它,但沒有希望,當我刪除黑色線條時,我也失去了陰影效果。 –

+0

9貼片圖像來自Android來源。 9-patch drawable中的黑線是有原因的,請閱讀更多關於9-patch images [here](http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch )。 – Tomik

1

我玩周圍與其他可能性的比特但使用9膜片與固定利潤和發現該層列表抽拉被允許以限定偏移量,因此,邊緣圍繞它的封閉繪項目,所以這個工作對我來說:

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

,然後你可以使用這個作爲「機器人:windowBackground」:

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

,而無需創建一個自定義背景繪製並加入了特殊的風格只需添加一行代碼:

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

優雅的解決方案。我將drawable資源設置爲我創建的自定義drawable,而不是透明的。 Thanx – BLOB

+0

謝謝。好的解決方案 – Dipali

+0

好的解決方案,如果不需要在多個類中使用它。 – CiDsTaR

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