0

有没有一种方法可以动态设置默认警报对话框上显示的图标而无需创建自定义图标?例如,我想要下面的 alertDialog,方法 setIcon() 来显示我使用 uri 变量提供的图像,其中存储了路径。

private void showProductInfo(){
    MyProduct myProduct= (MyProduct) myProductGoldenRetriever();
    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    alertDialog.setTitle("Product Information");
    alertDialog.setMessage(myProduct.getMyProductInfo());
    alertDialog.setButton("Back", new DialogInterface.OnClickListener() {
       @Override
        public void onClick(DialogInterface dialog, int which) {
           showPrompt();
       }
    });
    alertDialog.setIcon(R.drawable.default_img);//<--Here Need to Provide a different image each time
    alertDialog.show();
}//endOfShowProductInfo

这是可能的还是我需要使用适当的 .xml 布局文件创建自定义 alertDialog ?

4

1 回答 1

2

如果我理解正确,您只想将图标设置为从 Uri 创建的 Drawable?我假设MyProduct有一个getUri()方法。如果没有,只需相应地修改它:

Resources res = getResources();
BitmapDrawable icon = new BitmapDrawable(res, myProduct.getUri().toString());
alertDialog.setIcon(icon);

该类BitmapDrawable有一个接收资源的构造函数和一个表示文件路径的字符串。如果您使用的是 Uri,则可以使用toString(). 该类AlertDialog.Builder有一个重载setIcon()方法,它接受一个 Drawable。只需从您的路径创建您的可绘制对象,并将其设置为您的对话框图标。这没有经过测试(这里没有 Eclipse),但应该可以工作。

于 2011-01-04T15:40:04.013 回答