“Avoid passing null as the view root” warning when inflating view for use by AlertDialog

前端 未结 10 752
面向向阳花
面向向阳花 2020-12-12 15:41

I get the lint warning, Avoid passing null as the view root when inflating views with null as parent, like:

LayoutInfl         


        
10条回答
  •  忘掉有多难
    2020-12-12 16:26

    According to https://developer.android.com/guide/topics/ui/dialogs

    Inflate and set the layout for the dialog
    Pass null as the parent view because its going in the dialog layout

    therefore, for creating AlertDialog, I use @SuppressLint("InflateParams")

    LayoutInflater inflater = requireActivity().getLayoutInflater();
    @SuppressLint("InflateParams")
    View view = inflater.inflate(R.layout.layout_dialog, null);
    builder.setView(view);
    

提交回复
热议问题