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クラスには、Drawable を受け取るオーバーロードさsetIcon()れたメソッドがあります。パスからドローアブルを作成し、ダイアログ アイコンとして設定するだけです。これはテストされていませんが (ここでは Eclipse はありません)、動作するはずです。

于 2011-01-04T15:40:04.013 に答える