2015-05-13 101 views
1

我想創建一個AlertDialog作爲DialogFragment,它帶有標題,確定按鈕,取消按鈕和ExpandableListView。問題是,ExpandableListView需要儘可能多的空間,並按下按鈕和標題的對話框。我想要的是頂部的標題,底部的按鈕以及ExpandableListView將所有剩餘空間全部放在屏幕上,以便DialogFragment在展開時不會增加/減少尺寸,而是保留它滾動。控制AlertDialog中的視圖大小

這裏是描述情況的圖片,左邊是已初始化的DialogFragment,第二張是展開ExpandableListView的其中一個部分之後的圖片。沒有想到醜陋。

ExpandableListView expands too much.

我想實現如下:

  1. 保持固定在FragmentDialog的大小,優選地對整個窗口(fill_parent/match_parent)。
  2. 保持按鈕固定在底部,標題固定在頂部,ExpandableListView固定(但仍可滾動)在中心。

我已經嘗試了很多種不同的東西,但這裏是我目前的需要。

定製DialogFragment

public class RecipeFilterDialogFragment extends DialogFragment { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     builder.setMessage(R.string.recipe_filter_title); 
     builder.setPositiveButton(R.string.recipe_filter_button_ok, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       // TODO: Perform filtering, fill list and return. 
      } 
     }); 
     builder.setNegativeButton(R.string.recipe_filter_button_cancel, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int id) { 
       // TODO: Kill dialog and return. 
      } 
     }); 

     builder.setView(getActivity().getLayoutInflater().inflate(R.layout.recipe_filter_dialog, null)); 
     builder.setCancelable(true); 
     return builder.create(); 
    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     AlertDialog dialog = (AlertDialog) getDialog(); 
     if (dialog != null) 
     { 
      int width = ViewGroup.LayoutParams.MATCH_PARENT; 
      int height = ViewGroup.LayoutParams.MATCH_PARENT; 
      //dialog.getWindow().setLayout(width, height); 
      dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     } 
    } 
} 

爲DialogFragment(recipe_filter_dialog.xml)的xml文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <com.my.app.RecipeFilterExpandableListView 
     android:id="@+id/recipe_filter_expandableListView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
    /> 

</LinearLayout> 

定製ExpandableListView

public class RecipeFilterExpandableListView extends ExpandableListView { 

    public RecipeFilterExpandableListView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     this.setOnGroupExpandListener(new RecipeFilterDialogOnGroupExpandListener(this)); 

     // This adapter just fills the ExpandableListView, nevermind it. 
     this.setAdapter(new RecipeFilterExpandableListAdapter(context, ((MyActivity)context).getDbFilter())); 
    } 
} 
+0

爲視圖提供了一個固定的佈局高度和寬度 – 3xplore

回答

1

嘗試設置builder.setTitle(R.string.recipe_filter_title);而不是builder.setMessage(R.string.recipe_filter_title);,然後決定爲Dialog設置什麼高度和寬度,並將其設置爲onStart()方法。

也試圖做這樣的事情:

構造函數在對話框類:

public RecipeFilterDialogFragment() { 
     setStyle(DialogFragment.STYLE_NO_FRAME, R.style.FullscreenStyle); 
    } 

和風格爲這個對話框,用於以上:

<style name="FullscreenStyle" parent="@android:style/Theme.Holo.Light"> 
    <item name="android:windowIsFloating">false</item> 
    <item name="android:windowFullscreen">true</item> 
</style> 

也嘗試使用父母風格作爲對話主題fe:

<style name="FullscreenStyle" parent="@android:style/Theme.Holo.Light.Dialog"> 
+0

這個樣式確實沒有影響對話框的大小,但是使用'setTitle'確實改變了一些次要的東西(只是視覺效果)。你如何建議我在'onStart()'中計算和設置對話框的高度?我無法真正找到實現它的方法,我猜想它對於對話框和其中的「RecipeFilterExpandableListView」都是必需的。哦,並且在'RecipeFilterExpandableListView'的xml文件中設置'android:layout_height =「300dp」'確實會給出所需的行爲,但並沒有真正的幫助,因爲它是硬編碼的。 –

+0

如果您確實想要創建全屏對話框,只需按設備屏幕大小計算寬度和高度即可。 [以像素爲單位獲取屏幕尺寸](http://stackoverflow.com/a/1016941/4675547),然後樣式將影響對話框的大小。我希望這將有所幫助。 – Wisnia

相關問題