如何在BlurDialogFragment中添加自定义视图

时间:2016-09-17 13:22:27

标签: android dialog alertdialog android-alertdialog

我需要在对话框屏幕后面模糊背景。我找到了使用此链接https://github.com/tvbarthel/BlurDialogFragment的良好解决方案,但在此库中未指定如何集成自定义视图。这里的代码显示我们需要在OnCreateDialog方法中创建AlertDialog,但是我们可以在onCreateDialog方法中使用setcontentview()。

 @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = getActivity().getLayoutInflater().inflate(R.layout.dialog_fragment, null);
        TextView label = ((TextView) view.findViewById(R.id.textView));
        label.setMovementMethod(LinkMovementMethod.getInstance());
        Linkify.addLinks(label, Linkify.WEB_URLS);
        builder.setView(view);
        return builder.create();
    }

1 个答案:

答案 0 :(得分:1)

您可以创建一个扩展BlurDialogFragment的类,并且在onCreateView方法中可以扩展您的自定义布局。 请参阅以下示例:

public class CustomDialogFragment extends BlurDialogFragment {


@Override
protected boolean isActionBarBlurred() {
    // Enable or disable the blur effect on the action bar.
    // Disabled by default.
    return true;
}

@Override
protected int getBlurRadius() {
    // Allow to customize the blur radius factor.
    return 7;
}

@Override
protected boolean isDimmingEnable() {
    // Enable or disable the dimming effect.
    // Disabled by default.
    return false;
}


@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.dialog_fragment_layout, container, false);


    return v;
}

显示您的活动对话框:

FragmentManager fragmentManager = getFragmentManager();
CustomDialogFragment cdf = new CustomDialogFragment();
cdf.show(fragmentManager,"yourTag");