PopupWindowDemo.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
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.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.PopupWindow; public class PopupWindowDemo extends Activity { private Button btn; PopupWindow mPopupWindowSmall, mPopupWindowBig; ImageButton btnSmall, btnBig; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Context mContext = PopupWindowDemo.this; btnBig = new ImageButton(mContext); btnBig.setImageResource(R.drawable.big); btnBig.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub setTitle("big"); } }); mPopupWindowBig = new PopupWindow(btnBig, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); mPopupWindowBig.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 0, 0); btnSmall = new ImageButton(mContext); btnSmall.setImageResource(R.drawable.small); btnSmall.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub setTitle("small"); } }); mPopupWindowSmall = new PopupWindow(btnSmall, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); mPopupWindowSmall.showAtLocation(findViewById(R.id.main), Gravity.CENTER, 60, 0); } }); Button hide = (Button) findViewById(R.id.btn1); hide.setOnClickListener(new OnClickListener() { public void onClick(View v) { mPopupWindowSmall.dismiss(); mPopupWindowBig.dismiss(); } }); } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Welcome to Mr Wei's Blog!!"/> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Show PopupWindow"/> <Button android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="hide PopupWindow"/> </LinearLayout> |