2013-02-18 102 views
21

我一直在嘗試很多命令來設置我的DialogFragment的大小。它只是有一個顏色選擇器,所以我已刪除的對話框的背景和標題:設置DialogFragment的大小

getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
getDialog().getWindow().setBackgroundDrawable(
    new ColorDrawable(android.graphics.Color.TRANSPARENT)); 

不過我也想,我想對話框定位,這是有問題的。我使用:

WindowManager.LayoutParams params = getDialog().getWindow().getAttributes(); 
params.width = LayoutParams.WRAP_CONTENT; 
params.height = LayoutParams.WRAP_CONTENT; 
params.gravity = Gravity.LEFT; 
getDialog().getWindow().setAttributes(params); 

但是一個(大)的障礙仍然是:即使我的對話框窗格中是看不見的,它還是有一定的規模,並限制我對話的立場。 LayoutParams.WRAP_CONTENT在這裏將此窗格的大小限制爲我的顏色選擇器,但由於某種原因,它不起作用。

有沒有人能夠做類似的事情?

+1

尺寸和對話框的位置需要在Dialog構造函數中設置。您必須重寫DialogFragment的onCreateDialog以及您必須創建自定義對話框的位置。 – 2013-02-19 00:15:24

+1

+ Sudar Nimalan謝謝,這是解決方案的一部分。 – Teovald 2013-02-22 16:46:58

+3

對於將來的讀者,相關問題: 1)[設置DialogFragment的大小(寬度和高度)](http://stackoverflow.com/questions/12478520/how-to-set-dialogfragments-width-and-height) 2)[Positioning DialogFragment](http://stackoverflow.com/questions/9698410/position-of-dialogfragment-in-android) – Jonik 2014-02-26 20:20:33

回答

20

經過一些試驗和錯誤,我找到了解決辦法。

這裏是我DialogFragment類的實現:

public class ColorDialogFragment extends SherlockDialogFragment { 

    public ColorDialogFragment() { 
    //You need to provide a default constructor 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, 
        ViewGroup container, 
        Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.dialog_color_picker, container); 
    // R.layout.dialog_color_picker is the custom layout of my dialog 
    WindowManager.LayoutParams wmlp = getDialog().getWindow().getAttributes(); 
    wmlp.gravity = Gravity.LEFT; 
    return view; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setStyle(DialogFragment.STYLE_NO_FRAME, R.style.colorPickerStyle); 
    // this setStyle is VERY important. 
    // STYLE_NO_FRAME means that I will provide my own layout and style for the whole dialog 
    // so for example the size of the default dialog will not get in my way 
    // the style extends the default one. see bellow.   
    } 

    } 

R.style.colorPickerStyle對應於:

<style name="colorPickerStyle" parent="Theme.Sherlock.Light.Dialog"> 
    <item name="android:backgroundDimEnabled">false</item> 
    <item name="android:cacheColorHint">@android:color/transparent</item> 
    <item name="android:windowBackground">@android:color/transparent</item> 
</style> 

我簡單地擴展默認的對話風格我的需要。

最後,你可以調用此對話框:

private void showDialog() { 
    ColorDialogFragment newFragment = new ColorDialogFragment(); 
    newFragment.show(getSupportFragmentManager(), "colorPicker"); 
    } 
+0

非常感謝你!我想問你,你在做什麼關於將對話放在你想要的位置?你能做到嗎,怎麼做? – 2013-04-15 17:23:56

+1

在onCreateView中,您還可以通過以下方式訪問佈局的參數:WindowManager.LayoutParams wmlp = getDialog()。getWindow()。getAttributes(); 。然後,例如,如果要將對話框放在屏幕的左側,只需使用wmlp.gravity = Gravity.LEFT;那麼wmlp.x = this.getResources()。getDimensionPixelOffset(R.dimen.dialog_position); – Teovald 2013-04-15 21:27:42

+1

真棒@Teovald,爲你提供最好的體驗。非常感謝你! – 2013-04-15 23:56:01

41

我遇到了類似的問題,那就是你不能設置dialogFragment的寬度在代碼中的高度,經過多次嘗試,我發現了一個解決方案;

這裏是步驟來定製DialogFragment:從XML

1.inflate自定義視圖上的方法

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) 
{ 

    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
    getDialog().setCanceledOnTouchOutside(true); 

    View view = inflater.inflate(R.layout.XXX, 
      container, false); 
    //TODO:findViewById, etc 
    return view; 
} 

2.設置你的對話框的寬度的onResume()的高度,remrember中的onResume()/在onStart(),似乎沒有其他方法

public void onResume() 
{ 
    super.onResume(); 
    Window window = getDialog().getWindow(); 
    window.setLayout(width, height); 
    window.setGravity(Gravity.CENTER); 
    //TODO: 
} 
+1

最簡單的解決方案! – 2013-11-18 15:02:57

+0

這也適用於我。出於某種原因,我的頂部/底部邊距被忽略,但我將其更改爲填充並適用於我。 – SilithCrowe 2014-04-07 19:15:03

+0

也爲我工作。謝謝 – Munazza 2014-04-10 10:22:12

5

工作,爲我所用的情況下,我希望DialogFragment匹配的項目列表的大小。片段視圖是名爲fragment_sound_picker的佈局中的RecyclerView。我在RecyclerView周圍添加了一個包裝器RelativeLayout。

我已經用R.attr.listItemPreferredHeight在一個名爲item_sound_choice的佈局中設置了單個列表項視圖的高度。

DialogFragment從膨脹的View的RecyclerView中獲取LayoutParams實例,將LayoutParams高度調整爲列表長度的倍數,並將修改的LayoutParams應用於膨脹的父視圖。

結果是DialogFragment完美包裝了選擇的短名單。它包括窗口標題和取消/確定按鈕。

這裏是在DialogFragment設置:

// SoundPicker.java 
// extends DialogFragment 
@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder.setTitle(getActivity().getString(R.string.txt_sound_picker_dialog_title)); 

    LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); 
    View view = layoutInflater.inflate(R.layout.fragment_sound_picker, null); 
    RecyclerView rv = (RecyclerView) view.findViewById(R.id.rv_sound_list); 
    rv.setLayoutManager(new LinearLayoutManager(getActivity())); 

    SoundPickerAdapter soundPickerAdapter = new SoundPickerAdapter(getActivity().getApplicationContext(), this, selectedSound); 
    List<SoundItem> items = getArguments().getParcelableArrayList(SOUND_ITEMS); 
    soundPickerAdapter.setSoundItems(items); 
    soundPickerAdapter.setRecyclerView(rv); 
    rv.setAdapter(soundPickerAdapter); 

    // Here's the LayoutParams setup 
    ViewGroup.LayoutParams layoutParams = rv.getLayoutParams(); 
    layoutParams.width = RelativeLayout.LayoutParams.MATCH_PARENT; 
    layoutParams.height = getListItemHeight() * (items.size() + 1); 
    view.setLayoutParams(layoutParams); 

    builder.setView(view); 
    builder.setCancelable(true); 

    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { 
     // ... 
    }); 

    builder.setPositiveButton(R.string.txt_ok, new DialogInterface.OnClickListener() { 
     // ... 
    }); 

    return builder.create(); 
} 

@Override 
public void onResume() { 
    Window window = getDialog().getWindow(); 
    window.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    super.onResume(); 
} 

private int getListItemHeight() { 
    TypedValue typedValue = new TypedValue(); 
    getActivity().getTheme().resolveAttribute(R.attr.listPreferredItemHeight, typedValue, true); 
    DisplayMetrics metrics = new android.util.DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); 
    return (int) typedValue.getDimension(metrics); 
} 

這是fragment_sound_picker

<?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="wrap_content"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/rv_sound_list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 
</RelativeLayout> 
2

使用此代碼爲對話片段的大小調整的Android

public void onResume() { 
     super.onResume(); 

     super.onResume(); 
     Window window = getDialog().getWindow(); 
     window.setLayout(250, 100); 
     window.setGravity(Gravity.RIGHT); 
    } 
+0

我認爲寬度和高度取決於屏幕大小,但我真的找不到模式,因爲我在將對話框片段設置爲大小時遇到​​問題。 如果我設置高度150,並且如果獲得片段的高度,它會給我其他答案。請幫助我在這個問題https://stackoverflow.com/questions/45494644/how-to-set-the-height-of-the-dialog-fragment-accurately-in-android – 2017-08-03 22:53:49