问题
I am trying to set image view in android dialog box. If I select an image from gallery it shown in dialog with selected image. I will tried to get image from galley and passed to path to alert dialog.
回答1:
AlertDialog.Builder ImageDialog = new AlertDialog.Builder(MainActivity.this);
ImageDialog.setTitle("Title");
ImageView showImage = new ImageView(MainActivity.this);
ImageDialog.setView(showImage);
ImageDialog.setNegativeButton("ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface arg0, int arg1)
{
}
});
ImageDialog.show();
回答2:
hey follow this link..
First you have to set your custom dialog layout to your dialog box, like below.
setcontentview(R.layout.custom)
and set the image with setImageResources(your image id)
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
回答3:
For setting image in an alert dialog box you need to create a custom dialog like this
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/my_image"/>
</LinearLayout>
Then in your activity show your custom dialog box like this
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialogView = inflater.inflate(R.layout.dialog, null);
builder.setView(dialogView)
.setPositiveButton(R.string.create, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).create().show();
回答4:
I use this simple method for showing AlertDialog:
private void showAlertDialog(Context mContext, String mTitle, String mBody, int mImage){
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
builder.setCancelable(true);
builder.setIcon(mImage);
if(mTitle.length()>0)
builder.setTitle(mTitle);
if(mBody.length()>0)
builder.setTitle(mBody);
builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.create().show();
}
call method:
showAlertDialog(mContext, "OOPS!", getString(R.string.massage_nointernet), R.drawable.ic_no_internet);
来源:https://stackoverflow.com/questions/35570805/how-to-set-image-view-in-android-dialog-box