将自定义图像添加到Dialog

时间:2013-05-09 11:24:02

标签: android dialog android-imageview qr-code

我试图做的是一个应用程序,读取QR-CODES(参考图像),阅读后,显示一个对话框,其中包含与QR-CODE相关的图像和数据库的描述。

关于如何做到这一点的任何建议?

我在互联网上找到的只是如何使用自定义布局创建Dialog。没什么。

3 个答案:

答案 0 :(得分:2)

我假设您需要在自定义对话框中显示图像

定义dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

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

</RelativeLayout>

在您的活动按钮上单击显示一个对话框

 Button b= (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showpopup();        
        }

    });

   public void showpopup()
   {
   Dialog d = new Dialog(MainActivity.this);
   d.setContentView( R.layout.dialog);
   d.setTitle("my title");
   ImageView iv = (ImageView) d.findViewById(R.id.imageView1);
   iv.setImageResource(R.drawable.afor); 
   // set your image here. I have used a resource from the drawable folder
   d.show();
   }

enter image description here

答案 1 :(得分:0)

您可以使用setContentView() n将ImageView作为参数传递给它。

答案 2 :(得分:0)

对于自定义对话框,您只需创建一个布局并在对话框中设置该布局。

e.g

dialog.setContentView(R.layout.customdialog);

请参阅此示例Click Here.

相关问题