0

我正在处理一个 ImageViews 列表,虽然我希望每个都被点击,但图像会以弹出窗口的形式出现。目前取得了不错的效果,但工作量很大,我为每个图像创建了一个新的 xml 文件,因此使用 onclicklistener,每次单击 ImageView 时,都会打开另一个布局 xml,但我只想打开一个弹出图像。谁能帮我?

这是可能的xml:

资源/布局/main.xml

   <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="353dp"
    android:layout_marginTop="150dp" >

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical" >


   <Button
   android:id="@+id/button1"
   android:layout_width="fill_parent"
   android:layout_height="100dp"
   android:text="@string/popup1" />

   <Button
   android:id="@+id/button2"
   android:layout_width="fill_parent"
   android:layout_height="100dp"
   android:text="@string/popup2" />

   <Button
   android:id="@+id/button3"
   android:layout_width="fill_parent"
   android:layout_height="100dp"
   android:text="@string/popup3" />

   <Button
   android:id="@+id/button4"
   android:layout_width="fill_parent"
   android:layout_height="100dp"
   android:text="@string/popup4" />

   <Button
   android:id="@+id/button5"
   android:layout_width="fill_parent"
   android:layout_height="100dp"
   android:text="@string/popup5" />
   </LinearLayout>
   </ScrollView>
   </FrameLayout>

水库/布局/开胃酒

   <ImageView
   android:id="@+id/ivcarnes"
   android:layout_width="364dp"
   android:layout_height="410dp"
   android:layout_gravity="center"
   android:contentDescription="@string/panecestino"
   android:src="@drawable/panecestinoim" />

   <Button
   android:id="@+id/btclose"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_gravity="right"
   android:layout_marginRight="85dp"
   android:layout_marginTop="150dp"
   android:text="@string/close"
   android:textStyle="bold"/>
   </FrameLayout>

这是我的代码:Main.java

   import android.os.Bundle;
   import android.app.Activity;
   import android.content.Intent;
   import android.view.Menu;
   import android.view.View;
   import android.widget.*;

   public class Main extends Activity {

   Button button1, button2, button3, button4, button5;


   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

            button1 = (Button)
            findViewById(R.id.button1);
            button2 = (Button)
            findViewById(R.id.button2);
            button3 = (Button)
            findViewById(R.id.button3);
            button4 = (Button)
            findViewById(R.id.button4);
            button5 = (Button)
            findViewById(R.id.button5);

   button1.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View Arg0) {

   Intent testegrafico = new
   Intent(Main.this,Aperitivos.class);
   Main.this.startActivity(testegrafico);
   Main.this.finish();
   }});
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {

   getMenuInflater().inflate(R.menu.main, menu);
   return true;
   }}

开胃酒.java

   import android.os.Bundle;
   import android.app.Activity;
   import android.content.Intent;
   import android.view.Menu;
   import android.view.View;
   import android.widget.*;

   public class Aperitivos extends Activity {

   Button btclose;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.aperitivos);

   btclose = (Button)
   findViewById(R.id.btclose);

   btclose.setOnClickListener(new View.OnClickListener() {

   @Override
   public void onClick(View Arg0) {

   Intent testegrafico = new
   Intent(Aperitivos.this,Main.class);
   Aperitivos.this.startActivity(testegrafico);
   Aperitivos.this.finish();
   }
   });
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {

   getMenuInflater().inflate(R.menu.main, menu);
   return true;
   }

   }
4

1 回答 1

1
package br.com.example.locandaristorante;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

public class Main extends Activity {

Button button1, button2, button3, button4, button5;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

            button1 = (Button)
            findViewById(R.id.button1);
            button2 = (Button)
            findViewById(R.id.button2);
            button3 = (Button)
            findViewById(R.id.button3);
            button4 = (Button)
            findViewById(R.id.button4);
            button5 = (Button)
            findViewById(R.id.button5);

            button1.setOnClickListener(new Button.OnClickListener() {

 @Override
 public void onClick(View arg0) {
     View popupView = new View(Main.this);
     final PopupWindow popupWindow = new PopupWindow(popupView,
     ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
     Drawable drawable = getResources().getDrawable(R.drawable.panecestinoim);

     popupWindow.setBackgroundDrawable(drawable);
     popupWindow.setHeight(600);
     popupWindow.setWidth(400);      
     popupWindow.setTouchable(true);
     popupWindow.setFocusable(true);
     popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                popupWindow.dismiss();
                return false;
            }
        });



     popupWindow.showAtLocation(findViewById(R.id.button1), Gravity.CENTER, 0, 0);



                }
            });
}}
于 2013-04-15T20:40:40.673 回答