DialogFragment:如何设置展示位置和尺寸?

时间:2016-04-22 12:32:40

标签: android android-fragments

在我的应用程序中,我需要创建一个像这样的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);
    }

但我不确定如何设置DialogBox的高度并将其保留在屏幕的底部?

enter image description here

4 个答案:

答案 0 :(得分:0)

创建片段时,您可以选择说明要将片段布局添加到的位置。

add(R.layout....)

在活动布局中创建一个空布局,该布局是该框的已定义大小,然后只需将add方法更改为该布局

 add(R.layout.theNewlyCreatedLayoutWiththeRightSizeAndPosition)

答案 1 :(得分:0)

您可以使用onCreateDialog(Bundle 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;
}

答案 2 :(得分:0)

您可以使用Bottomsheet解决问题

Bottomsheet中,您还可以使用FragmentViews

Bottomsheet示例为here

答案 3 :(得分: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);
}