如何将图像放入 AlertDialog?安卓

新手上路,请多包涵

我不知道如何将图像放入 AlertDialog。

我有这个代码,但我认为这是不可能的。

 AlertDialog.Builder alert = new AlertDialog.Builder(MessageDemo.this);
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(R.drawable.cw);
alert.setView(imageView);
alert.setNeutralButton("Here!", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dlg, int sumthin) {

    }
});
alert.show();

原文由 fr4n 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 563
2 个回答

创建一个 sample.xml 并在该 XML 中添加 ImageView

示例.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">

    <ImageView
        android:id="@+id/dialog_imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

Java 代码:

 AlertDialog.Builder alertadd = new AlertDialog.Builder(MessageDemo.this);
LayoutInflater factory = LayoutInflater.from(MessageDemo.this);
final View view = factory.inflate(R.layout.sample, null);
alertadd.setView(view);
alertadd.setNeutralButton("Here!", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dlg, int sumthin) {

                }
            });

alertadd.show();

原文由 Niranj Patel 发布,翻译遵循 CC BY-SA 3.0 许可协议

试试这个方法:

 private void dialog() {
    AlertDialog.Builder alert = new AlertDialog.Builder(MainMenuActivity.this);
    LayoutInflater inflater = this.getLayoutInflater();
    View team = inflater.inflate(R.layout.image_layout, null);
    alert.setView(team);
    alert.show();
}

此外,实现一个名为 image_layout 的布局,并将所需的 UI 元素放入其中。

原文由 Ahmer Afzal 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏