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;

							}
						}
					});
				}
			}
		});

	}


  



posted @   程序员Android的博客  阅读(63)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
点击右上角即可分享
微信分享提示