确保以下几个步骤来解决在DialogFragment中无法显示ImageView的问题:
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_dialog, container, false);
LinearLayout dialogLayout = rootView.findViewById(R.id.dialog_layout);
...
return rootView;
}
ImageView imageView = new ImageView(getActivity());
imageView.setImageResource(R.drawable.your_image);
dialogLayout.addView(imageView);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
imageView.setLayoutParams(layoutParams);
return rootView;
这样,ImageView就会在DialogFragment中正确显示了。