将图像动态加载到对话框中

时间:2013-02-06 20:49:48

标签: android dialog android-dialogfragment

如何将图像动态加载到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);

图像作为空对象返回

1 个答案:

答案 0 :(得分: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);
}

希望这有帮助!