dialogfragment上显示dialogfragment的坑

上周写dialogfragment点击出现的dialogfragment的时候,出现的dialogfragment非常小,而且布局的宽高参数没起作用,先以为是之前的dialogfragment没关闭,后来先关闭再显示也不行,最后发现是需要在onstart方法里进行设置window的宽高.
代码如下:


public class BottomDialogFragment extends DialogFragment {


    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        View bottomView = inflater.inflate(R.layout.dialog_bottom_view, container, false);
        bottomView.findViewById(R.id.tv_change_password).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"修改密码",Toast.LENGTH_SHORT).show();
                BottomDialogFragment.this.dismiss();
                new CenterChangePasswordDialogFragment().show(getActivity().getSupportFragmentManager(),"center_change");
            }
        });
        bottomView.findViewById(R.id.delete_account).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BottomDialogFragment.this.dismiss();
                Toast.makeText(getActivity(),"删除账户",Toast.LENGTH_SHORT).show();
            }
        });
        return bottomView;
    }
}

完善下,由于要实现下面的效果:
dialogfragment上显示dialogfragment的坑_第1张图片
所以这里window的width和height参数设定就不能写成那样,改成如下:

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
//        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
//        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.width = ScreenUtils.dip2px(getActivity(),311);
        params.height = ScreenUtils.dip2px(getActivity(),230);
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    }

dialog的布局文件如下:


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginBottom="10dp"
    android:layout_height="match_parent"
    android:background="@drawable/bg_rect_radius8_white">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:textSize="16sp"
        android:layout_weight="1"
        android:text="@string/change_password"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"

        android:textStyle="bold"
        android:textColor="@color/black191"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:textSize="14sp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:textColor="@color/black_10"
        android:hint="@string/please_input_new_password"
        android:gravity="center"
        android:background="@null"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:textSize="14sp"
        android:background="@null"
        android:gravity="center"
        android:textColor="@color/black_10"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"

        android:hint="@string/please_confirm_new_passsword"
        android:layout_weight="1"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:gravity="center"
        android:layout_marginLeft="36dp"
        android:layout_marginRight="36dp"
        android:textSize="16sp"
       android:textColor="@color/white_fafafa"
        android:layout_weight="1"
        android:layout_marginBottom="22dp"
        android:background="@drawable/bg_rect_radius59_red"
        android:text="@string/confirm_to_be_changed"/>

LinearLayout>

你可能感兴趣的:(Android)