android中popupwindow的点滴
java类:
package com.tony.PopupWindow; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.SimpleAdapter; import android.widget.Toast; public class PopupWindowActivity extends Activity { 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.main); 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); HashMap<String, String> map3 = new HashMap<String, String>(); map3.put("menuItemName", "关于"); list.add(map3); HashMap<String, String> map4 = new HashMap<String, String>(); map4.put("menuItemName", "搜索"); list.add(map4); HashMap<String, String> map5 = new HashMap<String, String>(); map5.put("menuItemName", "退出"); list.add(map5); myButton = (Button) findViewById(R.id.button); myButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (state == 1 && pop.isShowing()) { state = 0; pop.dismiss(); } else { /*** 弹出自定义的菜单 ***/ layout = getLayoutInflater().inflate(R.layout.menu_layout, null); menulist = (ListView) layout.findViewById(R.id.menulist); SimpleAdapter listAdapter = new SimpleAdapter( PopupWindowActivity.this, list, R.layout.menu_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(), myButton.getHeight() * 3 + 5); 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() { @Override 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() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { switch (arg2) { case 0: Toast.makeText(getApplicationContext(), "系统设置", Toast.LENGTH_SHORT).show(); pop.dismiss(); break; case 1: Toast.makeText(getApplicationContext(), "自动更新", Toast.LENGTH_SHORT).show(); pop.dismiss(); break; case 2: Toast.makeText(getApplicationContext(), "关于", Toast.LENGTH_SHORT).show(); pop.dismiss(); break; case 3: Toast.makeText(getApplicationContext(), "搜索", Toast.LENGTH_SHORT).show(); pop.dismiss(); break; case 4: Toast.makeText(getApplicationContext(), "退出", Toast.LENGTH_SHORT).show(); pop.dismiss(); break; } } }); } } }); } }
布局文件:
main.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:orientation="vertical" > <Button android:layout_width="100dp " android:layout_height="wrap_content" android:id="@+id/button" android:text="菜单" /> </LinearLayout>
menu_layout.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:background="@drawable/a2" > <ListView android:id="@+id/menulist" android:layout_width="fill_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
menu_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" > <TextView android:id="@+id/menuitem" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:textColor="#FFFFFF" android:textSize="20sp" /> </LinearLayout>
下面是运行的结果:
点击popupwindow会有相应的处理,点击返回键或者是popupwindow外部的区域popupwindow也会消失,这也是很多人想要的效果。
需要强调的一些事儿:
1.PopuWindow 的大小由下面代码控制;
new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
2.需要顺利让PopUpWindow dimiss(即点击PopuWindow之外的地方此或者back键PopuWindow会消失);PopUpWindow的背景不能为空。必须在popuWindow.showAsDropDown(v);或者其它的显示PopuWindow方法之前设置它的背景不为空:如下面两行代码:
ColorDrawablecd = new ColorDrawable(-0000); popu.setBackgroundDrawable(cd);
注意这里设置背景并不会覆盖xml文件定义的背景。
3.当有popuWindow.setFocusable(false);的时候,说明PopuWindow不能获得焦点,即使设置设置了背景不为空也不能点击外面消失,只能由dismiss()消失,但是外面的View的事件还是可以触发,back键也可以顺利dismiss掉。当设置为popuWindow.setFocusable(true);的时候,加上下面两行设置背景代码,点击外面和Back键才会消失。
ColorDrawable cd = new ColorDrawable(-0000); pop.setBackgroundDrawable(cd);
4.这里设置显示PopuWindow之后在外面点击是否有效。如果为false的话,那么点击PopuWindow外面并不会关闭PopuWindow。当然这里很明显只能在Touchable下才能使用。
ColorDrawable cd = new ColorDrawable(-0000); pop.setBackgroundDrawable(cd);
相关推荐
huha 2020-10-16
xfcyhades 2020-11-20
sgafdsg 2020-11-04
Michael 2020-11-03
fengyeezju 2020-10-14
ziyexiaoxiao 2020-10-14
业余架构师 2020-10-09
OuNuo0 2020-09-29
moses 2020-09-22
Angelia 2020-09-11
qinxu 2020-09-10
刘炳昭 2020-09-10
Nostalgiachild 2020-09-07
Nostalgiachild 2020-08-17
leavesC 2020-08-14
一青年 2020-08-13
AndroidAiStudy 2020-08-07
ydc0 2020-07-30
绿豆饼 2020-07-28