2

如何将图像动态加载到android中的对话框中

public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());       
        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.popup, null));
}

我只能将静态按钮和图像链接到视图中。

如果我尝试像动态链接图像

ImageView image = (ImageView) getActivity().findViewById(R.id.image);
image.setImageResource(R.drawable.icon);

图像作为空对象返回

4

1 回答 1

1

这是解决方案

public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());       
        LayoutInflater inflater = getActivity().getLayoutInflater();
        ViewGroup vg = (ViewGroup)inflater.inflate(R.layout.popup, null);
        ImageView image = (ImageView) vg.findViewById(R.id.image);
        image.setImageResource(R.drawable.icon);
        builder.setView(vg);
}

希望这可以帮助!

于 2013-02-06T21:33:14.620 回答