2011-03-23 45 views
18

根據此處的代碼, http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog。我成功地創建了一個帶有背景和按鈕的自定義對話框,但仍然有些不對。標題欄仍有空間,視圖周圍有邊框。如何擺脫這些標題和邊界?無標題和邊框的自定義對話框

這裏是我的對話框

Dialog pauseMenu = new Dialog(this,R.xml.pause_dialog); 
pauseMenu.setContentView(R.layout.pause_menu); 
return pauseMenu; 

,這裏是我的暫停佈局

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:background="@drawable/pause_menu_cropped" android:layout_gravity="center" android:gravity="center|center_horizontal"> 
    <TableLayout android:layout_width="wrap_content" android:id="@+id/tableLayout1" android:layout_height="wrap_content"> 
     <ImageButton android:src="@drawable/pause_button_quit" android:layout_width="wrap_content" android:background="@drawable/pause_button_quit" android:id="@+id/imageButton2" android:layout_height="wrap_content"></ImageButton> 
     <ImageButton android:src="@drawable/pause_button_option" android:layout_width="wrap_content" android:background="@drawable/pause_button_option" android:id="@+id/imageButton1" android:layout_height="wrap_content"></ImageButton> 
    </TableLayout> 
</LinearLayout> 
+0

你可以附上照片嗎? – 2011-03-23 15:18:41

+0

[如何在Android中創建完全自定義的對話/彈出窗口(更改疊加層顏色和對話框窗口布局)](http://stackoverflow.com/questions/3728990/how-to-create-a-completely-custom -dialogue-popup-android-change-overlay-colou) – 2011-03-23 15:27:47

+0

我剛剛發佈了一個類似的問題,下面清楚地回答瞭解決方案。 – mobibob 2011-06-09 03:30:44

回答

17

一個對話框不能沒有標題來創建。在該教程中進一步提到:

使用基類Dialog 類所做的對話框必須具有標題。如果您不調用setTitle() ,則標題使用的空間 保持空白,但仍可見 。如果您不想在 全部標題,那麼您應該使用AlertDialog 類創建您的 自定義對話框。但是,由於AlertDialog 最容易被 AlertDialog.Builder類創建,所以您不需要 就可以訪問上面使用的setContentView(int) 方法。相反,你必須 使用setView(View)。此方法接受一個View對象 ,因此您需要從 XML向 佈局的根視圖對象充氣。

This answer使用自定義樣式解決了標題和邊框問題。

+1

看起來像我對標題的理解,但是哪一部分對邊界有所瞭解? – Fugogugo 2011-03-23 15:48:45

+0

這條線在答案句柄: @ color/transparent_white 2011-03-23 15:51:03

+0

替代AlertDialog我們可以通過'final Dialog dialog = new Dialog(ShortcutActivity.this)來解決這個問題。 \t \t \t \t dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);' – 2015-09-09 09:50:49

89

我認爲這將幫助你

GAMEOVER將是對話的名稱和的setContentView這將是烏拉圭回合的自定義對話框佈局

gameOver = new Dialog(Main.this); 
gameOver.requestWindowFeature(Window.FEATURE_NO_TITLE); 
gameOver.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
gameOver.setCancelable(false); 
gameOver.setContentView(R.layout.gameover); 
+17

這應該是公認的答案。 +1 – 2012-06-05 16:41:47

+1

在Android版本低於4.1,對話框寬度涵蓋了全屏,這是不利於用戶界面 – 2013-08-22 08:36:06

+0

我同意@BasavarajHampali,這解決了問題,但寬度佔用了整個寬度...我不知道如何解決這個問題? – KarenAnne 2013-09-09 11:57:02

3
Dialog dialog = new Dialog(Main.this); 
dialog.getWindow(); 
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
9
  1. 作出這樣的類:

    public class CustomDialog extends Dialog { 
        public AlertFinishiView(Context context) { 
         super(context); 
         requestWindowFeature(Window.FEATURE_NO_TITLE); 
         setContentView(R.layout.dialog); 
         getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
        } 
    } 
    
  2. 使layut文件夾中的XML使用該名稱對話框

    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="@android:color/transparent"> 
    
        <RelativeLayout 
         android:layout_width="220dp" 
         android:layout_height="140dp" 
         android:layout_centerHorizontal="true" 
         android:layout_centerVertical="true"    
         android:background="@drawable/bg_custom_dialog" > 
    
         <Button android:id="@+id/button1" 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:layout_centerHorizontal="true" 
         android:layout_centerVertical="true" 
         android:text="Button" /> 
    
        </RelativeLayout> 
    
    </RelativeLayout> 
    
  3. 在繪製文件夾名稱bg_custom_dialog.9添加上面的圖片。PNG

bg_custom_dialog

在活動
  • 呼叫:

    CustomDialog customDialog = new CustomDialog(this); 
    customDialog.show(); 
    
  • 3
    Dialog dialog = new Dialog(context); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    
    1

    嘗試在styles.xml以下樣式:

    <style name="free_floating_dialog" parent="@android:style/Theme.Holo.Light.Dialog"> 
        <item name="android:windowBackground">@android:color/transparent</item> 
        <item name="android:windowIsFloating">true</item> 
        <item name="android:windowNoTitle">true</item> 
        <item name="android:windowCloseOnTouchOutside">false</item> 
    </style> 
    

    更多選擇參閱「Theme.Holo.Light.Dialog」,這是Android源代碼的一部分(自Honeycomb以來),或任何其他基於您的對話框樣式。

    然後簡單地使用這種風格的基礎上,以自己的對話框:

    Dialog myDialog = new Dialog(getActivity(), R.style.free_floating_dialog); 
    myDialog.setContentView(R.layout.myContent); 
    
    1

    使用此,它完美;

       dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); //before  
          dialog.setContentView(R.layout.your_dialog_layout); 
    
    0

    我用這個代碼,它很好地工作:

    AlertDialog.Builder builder = new AlertDialog.Builder(contextActivity,android.R.style.Theme_Holo_Dialog_NoActionBar); 
    AlertDialog alertDialog = builder.create(); 
    alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 
    alertDialog.setView(myLayout,0,0,0,0); 
    alertDialog.show();