android popwindows简单示例

最近用到popwindow,做了一个小Demo,效果如下


布局入下:popwindow.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="wrap_content"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/menulist"
        android:layout_width="100dp"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

  pop_list_item.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="wrap_content"
    android:layout_marginLeft="15dp"
    android:layout_marginTop="5dp"
    android:background="@color/black" >

    <TextView
        android:id="@+id/menuitem"
        android:layout_width="wrap_content"
        android:layout_height="40sp"
        android:gravity="center"
        android:textColor="#FFFFFF"
        android:textSize="20dp" />

</LinearLayout>

test.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:background="@color/white"
    android:orientation="vertical" >

    <Button
        android:id="@+id/shopbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试" />

</LinearLayout>

实现的代码如下:

	private static final String TAG = "PopupWindowActivity";

	private List<Map<String, String>> list = new ArrayList<Map<String, String>>();

	private int state;
	private Button myButton;
	private ListView menulist;
	private View layout;
	private PopupWindow pop;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test);

		popshop();

	}

	private void popshop() {

		HashMap<String, String> map1 = new HashMap<String, String>();
		map1.put("menuItemName", "店铺");
		list.add(map1);
		HashMap<String, String> map2 = new HashMap<String, String>();
		map2.put("menuItemName", "宝贝");
		list.add(map2);

		myButton = (Button) findViewById(R.id.shopbtn);

		myButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				if (state == 1 && pop.isShowing()) {
					state = 0;
					pop.dismiss();
				} else {
					/*** 弹出自定义的菜单 ***/
					layout = getLayoutInflater().inflate(R.layout.popwindow,
							null);
					menulist = (ListView) layout.findViewById(R.id.menulist);
					SimpleAdapter listAdapter = new SimpleAdapter(
							PopuwindowActivity.this, list,
							R.layout.pop_list_item,
							new String[] { "menuItemName" },
							new int[] { R.id.menuitem });
					menulist.setAdapter(listAdapter);

					/**
					 * layout PopupWindow所显示的界面 myButton.getWidth()
					 * 设置PopupWindow宽度 myButton.getHeight() * 3 + 5
					 * 设置PopupWindow宽度高度
					 */
					pop = new PopupWindow(layout, myButton.getWidth() + 40,
							myButton.getHeight() * 3 + 2);

					ColorDrawable cd = new ColorDrawable(-0000);
					pop.setBackgroundDrawable(cd);
					// pop.showAsDropDown(v);

					pop.update();
					pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
					pop.setTouchable(true); // 设置popupwindow可点击
					pop.setOutsideTouchable(true); // 设置popupwindow外部可点击
					pop.setFocusable(true); // 获取焦点
					/* 设置popupwindow的位置 */
					pop.showAtLocation(layout,
							(Gravity.BOTTOM - myButton.getHeight())
									| Gravity.LEFT, 0, 2 * myButton.getHeight());
					state = 1;
					pop.setTouchInterceptor(new View.OnTouchListener() {
						public boolean onTouch(View v, MotionEvent event) {
							/**** 如果点击了popupwindow的外部,popupwindow也会消失 ****/
							if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
								pop.dismiss();
								return true;
							}
							return false;
						}

					});
					/**** 点击listview中item的处理 ****/
					menulist.setOnItemClickListener(new OnItemClickListener() {

						public void onItemClick(AdapterView<?> arg0, View arg1,
								int arg2, long arg3) {
							switch (arg2) {
							case 0:
								myButton.setText("店铺");
								Toast.makeText(getApplicationContext(),
										"亲,你点击了店铺!", Toast.LENGTH_SHORT).show();
								pop.dismiss();
								break;
							case 1:
								myButton.setText("宝贝");
								Toast.makeText(getApplicationContext(),
										"亲,你点击了宝贝!", Toast.LENGTH_SHORT).show();
								pop.dismiss();
								break;

							}
						}
					});
				}
			}
		});

	}


  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员Android

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值