popup.java
package com.android.popup; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.PopupWindow; public class popup extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findviews(); setlisteners(); } private Button et; // private TextView tv; void findviews() { et=(Button)findViewById(R.id.btnSpw); //tv=(TextView)findViewById(R.id.tv); } void setlisteners() { //et.setOnKeyListener(et_keyDown); et.setOnClickListener(btnspw_click); //(4.toString()) //Byte.p } View.OnClickListener btnspw_click=new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub showPopupWindow(popup.this, findViewById(R.id.btnSpw)); } }; public void showPopupWindow(Context context,View parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); final View vPopupWindow=inflater.inflate(R.layout.popupwindow, null, false); final PopupWindow pw= new PopupWindow(vPopupWindow,300,300,true); //OK按钮及其处理事件 //Cancel按钮及其处理事件 Button btnCancel=(Button)vPopupWindow.findViewById(R.id.BtnCancel); btnCancel.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { pw.dismiss();//关闭 } }); //显示popupWindow对话框 pw.showAtLocation(parent, Gravity.CENTER, 0, 0); } }
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btnSpw" android:text="shop popup window" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> </LinearLayout>
popupwindow.xml
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <ImageView android:layout_weight="1" android:src="@drawable/exit_f" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ImageView> <ImageView android:layout_weight="1" android:src="@drawable/exit_f" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ImageView> <ImageView android:id="@+id/imageView1" android:src="@drawable/exit_f" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ImageView> <ImageView android:layout_weight="1" android:src="@drawable/exit_f" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ImageView> <ImageView android:layout_weight="1" android:src="@drawable/exit_f" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ImageView> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <Button android:id="@+id/BtnCancel" android:text="close popup window" android:layout_width="wrap_content" android:layout_height="wrap_content"> </Button> </LinearLayout> </LinearLayout>