2009-12-10 94 views
27

我使用AlertDialog.Builder創建了一個AlertDialog,但對話邊框在屏幕上佔用了太多空間。我如何刪除邊框?我嘗試過使用另一個Activity來模擬具有透明背景的對話框,但對話框反覆使用,並且每次創建一個新的Activity都會引入大量的延遲。Android無邊距對話框

here答案提到,它可以在ApiDemos被發現,但我似乎無法找到它。

回答

63

好吧,我會回答我自己的問題。基本上,不是使用AlertDialog.Builder,而是使用其構造函數創建常規對話框,並使用合適的主題而不是默認的Dialog主題。

所以您的代碼會是這個樣子:

Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar); 

希望這可以幫助其他人。

+4

如果我們這樣做,然後 dialog.setCanceledOnTouchOutside(true); 將不起作用...那麼如何處理呢? – amithgc 2010-08-13 06:50:56

+0

這正是我正在尋找的。我一直在用對話創建系統和幾十個寫得很差的教程摔角幾個小時,而你的一行代碼就是我所需要的。謝謝! – kodi 2010-09-03 13:37:13

+3

,但它使對話全屏幕..如何防止它不在全屏,因爲我寫上面的代碼 – AndroidDev 2011-11-18 05:32:15

24

如果您希望對話框全屏顯示,請使用android.R.style.Theme_Translucent_NoTitleBar。另一種方法是創建自己的風格,就像這樣:

<style 
    name="Theme_Dialog_Translucent" 
    parent="android:Theme.Dialog"> 
    <item 
     name="android:windowBackground">@null</item> 
</style> 
+0

+1這是工作。謝謝你,Jonasb。 – 2012-07-12 07:10:05

1

你可以要求製造商執行逆背景。爲我工作以顯示帶有PNG源的無邊界閃屏。

2

我增加了一個透明的像素繪製並用下面的代碼:

dialog.getWindow().setBackgroundDrawableResource(R.drawable.transpix); 
33

這裏是我的解決方案,來獲得一個對話框,只顯示你的內容。

Dialog dialog = new Dialog(this,R.style.ThemeDialogCustom); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 

    //you can move the dialog, so that is not centered 
    // dialog.getWindow().getAttributes().y = 50; //50 should be based on density 

    dialog.setContentView(yourLinearLayout); 
    dialog.setCancelable(true); 
    //dialog.setOnCancelListener(cancelListener); 
    dialog.show(); 

的themes.xml //位於項目/ RES /值

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="ThemeDialogCustom"> 
    <item name="android:windowFrame">@null</item> 
    <item name="android:windowIsFloating">true</item> 
    <item name="android:windowContentOverlay">@null</item> 
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> 
    <item name="android:windowBackground">@color/transparent_color</item> 
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> 
    <item name="android:colorBackgroundCacheHint">@null</item> 
    </style> 
</resources> 

colors.xml //也設在那裏

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
     <color name="transparent_color">#00000000</color> 
</resource> 
+4

這是一個不錯的解決方案。通過簡單地給你相同的對話框,它完美地解決了這個問題,但沒有邊界!這是比接受的更合適的解決方案。 – 2013-06-15 19:05:13

+0

感謝您的正確解決方案。並且您還可以添加dialog.setCanceledOnTouchOutside(true);關閉對話框外側的對話框。 – 2013-07-10 09:33:51

7

試試這個:d

Dialog popUpView= new Dialog(this); 
popUpView.getWindow().setBackgroundDrawable(new ColorDrawable(0)); 
2

如果你有2個邊框,你需要使用ContextThemeWrapp呃,它會顯示只有一個邊框,因爲你想:)

ContextThemeWrapper wrapper = new ContextThemeWrapper(this, android.R.style.Theme_Holo); 
final LayoutInflater inflater = (LayoutInflater) wrapper.getSystemService(LAYOUT_INFLATER_SERVICE); 
AlertDialog.Builder builder = new AlertDialog.Builder(wrapper); 
0

在您的資源文件中創建一個XML文件命名爲例如, null_image.xml,具有以下內容:

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

<solid android:color="#0000" /> 

<size 
    android:height="1dp" 
    android:width="1dp" /> 

</shape> 

在Java代碼中,獲取對話窗口,並設置xml文件作爲繪製資源,如: 取決於上下文:

Dialog dialog = new Dialog(getContext()); 
Window window = dialog.getWindow(); 
window.setBackgroundDrawableResource(R.drawable.null_image); 

就是這樣,享受。