自定义DialogFragment实现复杂Dialog

What

一种更加灵活的Dialog

Why

谷歌推荐,但是有时不能够满足需求,比如定制边距,突出关闭按钮,当然你也可以使用Dialog形式的Activity

目标效果:

这里写图片描述

How

1. 继承DialogFragment并重写onCreateDialog方法

public abstract class AbstractCustomDialogFragment extends DialogFragment{

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog alertDialog = new Dialog(getContext());
        alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

        View customView = onCreateDialogView();
        alertDialog.setContentView(customView);
        alertDialog.setCanceledOnTouchOutside(false);

        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        WindowManager.LayoutParams windowParams = alertDialog.getWindow().getAttributes();
        windowParams.width = (int) (getResources().getDisplayMetrics().widthPixels -
                        getResources().getDisplayMetrics().density * 20);
        alertDialog.getWindow().setAttributes(windowParams);

        return alertDialog;
    }

    //提供接口用来获取Dialog样式
    public abstract View onCreateDialogView();

}

2.继承AbstractCustomDialogFragment,重写onCreateDialogView方法

public class MyDialogFragment extends AbstractCustomDialogFragment {

    private View mView;

    @Override
    public View onCreateDialogView() {
        mView = LayoutInflater.from(getContext()).inflate(R.layout.fragment_my_dialog, null);
        mView.findViewById(R.id.imageView).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        return mView;
    }
}

3.在调用的地方调用

MyDialogFragment dialogFragment = new MyDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "FirstDialog");

4.对应的fragment_my_dialog.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    tools:context="study.ung.maitamaresearch.dialogs.MyDialogFragment">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="@dimen/margin_10dp"
        android:background="@color/transparent">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/hello_blank_fragment" />

    </RelativeLayout>


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/close" />
</RelativeLayout>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值