单击图像时关闭弹出窗口

时间:2012-01-10 07:11:00

标签: android popup

public void onListItemClick(ListView parent, View v, int position, long id) 
  {         
      super.onListItemClick(parent, v, position, id);



  LayoutInflater inflater = (LayoutInflater)
       this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 final PopupWindow pw = new PopupWindow(
       inflater.inflate(R.layout.offer_popup, null, false), 
       500, 
       600, 
       true);

 pw.showAtLocation(this.findViewById(android.R.id.list), Gravity.CENTER, 0, 0);    

ImageView closeimage=(ImageView) findViewById(R.id.imageView2);
closeimage.setOnClickListener(new OnClickListener() {

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

offer_popup.xml

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:theme="@android:style/Theme.Dialog"
android:orientation="vertical" android:weightSum="1">
 <RelativeLayout android:layout_width="fill_parent"
 android:id="@+id/relativeLayout_popup"
  android:layout_height="wrap_content" android:layout_weight="0.65">
    <ImageView android:layout_width="wrap_content"
     android:src="@drawable/node_largeview_black" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView1" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true"
      android:layout_marginTop="61dp"></ImageView>
    <ImageView android:id="@+id/imageView2" 
    android:layout_height="wrap_content" 
    android:src="@drawable/close_button" 
    android:layout_width="wrap_content" 
    android:layout_alignTop="@+id/imageView1"
     android:layout_alignRight="@+id/imageView1"></ImageView>
  </RelativeLayout>
 </LinearLayout>

我有一个列表视图,如果我点击其中一个项目行,将使用offer_popup.xml创建一个弹出窗口,在这个xml中,有一个imageView2 id是一个关闭图像,然后单击它,弹出窗口将驳回。

然而,该程序崩溃了closeimage = null。

3 个答案:

答案 0 :(得分:1)

View popView = inflater.inflate(R.layout.offer_popup, null, false);
final PopupWindow pw = new PopupWindow(popView
       , 
       500, 
       600, 
       true);
ImageView closeimage=(ImageView) popView.findViewById(R.id.imageView2);

更改您的代码,如果它不是主要布局的一部分,您需要为图像的父参考找到它。

答案 1 :(得分:1)

制作View offer_popup个xml文件并使用该视图查找ImageView ..

View popViewWindow = inflater.inflate(R.layout.offer_popup, null, false);  
ImageView closeimage=(ImageView)popViewWindow.findViewById(R.id.imageView2); 

答案 2 :(得分:1)

我认为您的closeimage不是您活动视图层次结构的一部分。尝试保存对offer_popup视图的引用并从那里获取closeimage:

View popupView = inflater.inflate(R.layout.offer_popup, null, false);
final PopupWindow pw = new PopupWindow(
   popupView, 
   500, 
   600, 
   true);
ImageView closeimage=(ImageView) popupView.findViewById(R.id.imageView2);
//...