2016-04-22 92 views
2

在我的應用程序中,我需要創建一個像這樣的DialogFragment(如圖所示 - 底部1/4屏幕)。我已經通過DialogFragment:如何設置放置和尺寸?

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     int style = DialogFragment.STYLE_NO_TITLE; 
     int theme = android.R.style.Theme_Holo; 
     setStyle(style, theme); 
    } 

achived全屏DialogFragment但我不知道如何設置對話框的高度,並保持它在屏幕的底部?

enter image description here

+0

你可以改變你的rootview的的LayoutParams的寬度/高度 – Blackbelt

+0

您可以使用底部片相同 –

+0

@BhaveshDesai底層可以包含菜單隻? – Alex

回答

0

當您創建的片段你在哪裏,你想要的片段佈局將被添加到給狀態的選項。

add(R.layout....) 

創建活動的佈局是定義框的大小,然後簡單地add方法更改爲佈局空的佈局

add(R.layout.theNewlyCreatedLayoutWiththeRightSizeAndPosition) 
0

您可以使用onCreateDialog(捆綁savedInstance)方法創建對話框,然後設置位置。例如 -

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    // Make the dialog view ready 
    //LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); 
    //View v = layoutInflater.inflate(R.layout.dialog,null); 

    // Create the dialog 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    // Set the view in dialog 
    //builder.setView(v); 
    Dialog dialog = builder.create(); 

    // Set the dialog position 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    WindowManager.LayoutParams wmlp = dialog.getWindow().getAttributes(); 

    wmlp.gravity = Gravity.BOTTOM| Gravity.CENTER; 
    wmlp.x = 100; //x position 
    wmlp.y = 100; //y position 


    return dialog; 
} 
0

您可以使用Bottomsheet您的問題

Bottomsheet你也可以使用FragmentViews

Bottomsheet例子是here

0

好,你可以做到這一點簡單地覆蓋OnCreateView和然後根據需要手動設置參數。

下面是一個例子

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) { 

    Window window = getDialog().getWindow(); 

    // set gravity 
    window.setGravity(Gravity.BOTTOM|Gravity.CENTER); 

    // then set the values to where you want to position it 
    WindowManager.LayoutParams params = window.getAttributes(); 
    params.x = 300; 
    params.y = 100; 
    window.setAttributes(params); 
}