2011-06-21 139 views
0

我建立包含自定義視圖像這樣的AlertDialog:的Android AlertDialog不居中/垂直縮放

@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
     case DIALOG_INTRO: 
      // setup the dialog 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      View inflatedLayout = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.intro, null); 
      builder.setView(inflatedLayout); 

      // return the dialog 
      return builder.create(); 
    } 

    return super.onCreateDialog(id); 
} 

而且佈局XML:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#000000"> 

    <TableLayout 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:stretchColumns="2" 
     android:shrinkColumns="1"> 

     <TableRow> 
      <TextView 
       android:id="@+id/intro_TextView_title" 
       android:layout_height="wrap_content" 
       android:layout_span="2" 
       android:gravity="center" 
       android:text="@string/intro_title" 
       android:textSize="22sp" 
       android:textColor="#FFFFFF" /> 
     </TableRow> 

     <TableRow 
      android:layout_marginTop="20dip" > 
      <ImageView 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:layout_marginLeft="25dip" 
       android:layout_marginRight="25dip" 
       android:paddingTop="2dip" 
       android:src="@drawable/intro_add" /> 
      <TextView 
       android:id="@+id/intro_TextView_add" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginRight="20dip" 
       android:text="@string/intro_add" 
       android:textColor="#FFFFFF" /> 
     </TableRow> 

     <!-- ... --> 

    </TableLayout> 
</ScrollView> 

這給了我一個對話框,長相這樣的事情(細節模糊了,不好意思):

blurred AlertDialog screenshot

問題在於頂部(在對話框上方)的巨大差距。我能做些什麼來擺脫它,或讓底部有類似的差距?

回答

-1

原來,這是一個錯誤(有點兒)。你必須實現你自己的對話框才能使它正確工作。

2

這裏描述的錯誤:http://code.google.com/p/android/issues/detail?id=11824。 AlertDialog即使在沒有標題和圖標的情況下也爲標題/圖標面板保留空間。

我認爲這個問題很簡單:它應該將頂部面板佈局設置爲GONE,就像在沒有按鈕的情況下它爲按鈕面板一樣。在這之前,唯一的解決方法是實現自己的Dialog子類。