最近用到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;
}
}
});
}
}
});
}