1、PopupWindow是一个可以显示在当前Activity之上的浮动容器,PopupWindow弹出的位置是能够改变的,按照有无偏移量,可以分为无偏移和有便宜两种;按照参照对象的不同又可以分为两种:相对某个控件(Anchor锚点)的位置和在父容器内部的相对位置。

  显示PopupWindow的方法:

  1. showAsDropDown(Viewanchor) //相对某个控件的位置(正下方),无偏移
  2. showAsDropDown(Viewanchor, int xoff, int yoff) //相对某个控件的位置,有偏移,xoff X轴的偏移量,yoff Y轴的偏移量
  3. showAtLocation(Viewparent, int gravity, int x, int y) //在父容器的什么位置,gravity为相对位置,如:正中央Gravity.CENTER、下方Gravity.BOTTOM、
  4. Gravity.Right|Gravity.BOTTOM右下方等,后面两个参数为x/y轴的偏移量。

2、创建泡泡窗口的界面:

  android:verticalSpacing="10px"    //垂直间隔 
    android:horizontalSpacing="10px" //横向间隔 
    android:numColumns="auto_fit"    //自适应列数 
    android:columnWidth="60px"        //列宽 
    android:stretchMode="columnWidth" //缩放与列宽大小同步

  convertView.setLayoutParams(new AbsListView.LayoutParams(45,45)); //有时候需要指定格子的宽度和高度

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="@drawable/bg"
  6. android:orientation="vertical" >
  7.  
  8. <GridView
  9. android:id="@+id/gridView"
  10. android:layout_width="match_parent"
  11. android:layout_height="match_parent"
  12. android:horizontalSpacing="10dp"
  13. android:numColumns="4"
  14. android:verticalSpacing="10dp" />
  15. </LinearLayout>

  为GridView控件设置背景色,在res/drawable目录创建bg.xml文件:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle" >
  4.  
  5. <gradient
  6. android:angle="270"
  7. android:endColor="#1DC9CD"
  8. android:startColor="#A2E0FB" />
  9.  
  10. <padding
  11. android:bottom="2dp"
  12. android:left="2dp"
  13. android:right="2dp"
  14. android:top="2dp" />
  15.  
  16. </shape>

  泡泡窗口由图片和文字组成,因此为它的项设置布局界面grid_item.xml文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:gravity="center"
  6. android:orientation="vertical" >
  7.  
  8. <ImageView
  9. android:id="@+id/imageView"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content" />
  12.  
  13. <TextView
  14. android:id="@+id/textView"
  15. android:layout_width="fill_parent"
  16. android:layout_height="wrap_content"
  17. android:gravity="center"
  18. android:textColor="#000099"
  19. android:textSize="16sp" />
  20.  
  21. </LinearLayout>

  3、为泡泡窗口设置动画效果:

  设置动画样式:

  1. <resources>
  2. <style name="AppBaseTheme" parent="android:Theme.Light">
  3. </style>
  4. <style name="AppTheme" parent="AppBaseTheme">
  5. </style>
  6. <style name="animation">
  7. <item name="android:windowEnterAnimation">@anim/enter</item>
  8. <item name="android:windowExitAnimation">@anim/out</item>
  9. </style>
  10. </resources>

  动画样式需要在res/anim下创建,需要创建进入(enter.xml)和退出(out.xml)两个动画:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shareInterpolator="false" >
  4. <!-- 设置平移,现在只需要在Y轴上移动 -->
  5. <translate
  6. android:duration="500"
  7. android:fromYDelta="100%p"
  8. android:toYDelta="0" />
  9. <!-- 设置透明度 -->
  10. <alpha
  11. android:duration="300"
  12. android:fromAlpha="0.7"
  13. android:toAlpha="1.0" />
  14.  
  15. </set>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shareInterpolator="false" >
  4.  
  5. <translate
  6. android:duration="3000"
  7. android:fromYDelta="0"
  8. android:toYDelta="100%p" />
  9.  
  10. <alpha
  11. android:duration="2000"
  12. android:fromAlpha="1.0"
  13. android:toAlpha="0.5" />
  14. </set>

  4、后台代码实现:

  1. package com.example.popupwindow;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.List;
  6.  
  7. import android.app.Activity;
  8. import android.graphics.drawable.BitmapDrawable;
  9. import android.os.Bundle;
  10. import android.view.Gravity;
  11. import android.view.Menu;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.AdapterView;
  15. import android.widget.AdapterView.OnItemClickListener;
  16. import android.widget.GridView;
  17. import android.widget.ListAdapter;
  18. import android.widget.PopupWindow;
  19. import android.widget.SimpleAdapter;
  20.  
  21. public class MainActivity extends Activity {
  22. PopupWindow popupWindow;
  23. View parent;
  24. private int[] images = { R.drawable.i1, R.drawable.i2, R.drawable.i3,
  25. R.drawable.i4, R.drawable.i5, R.drawable.i6, R.drawable.i7,
  26. R.drawable.i8 };
  27. private String[] names = { "搜索", "文件管理", "下载管理", "全屏", "网址", "书签", "加入书签",
  28. "分享页面" };
  29.  
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. // LayoutInflater mLayoutInflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
  35. //通过LayoutInflater获取布局界面
  36. View contentView = getLayoutInflater().inflate(R.layout.popupwin, null);
  37. //查找popupwin界面的GridView控件
  38. GridView gridView = (GridView) contentView.findViewById(R.id.gridView);
  39. //填充数据
  40. gridView.setAdapter(getAdapter());
  41. //设置点击事件
  42. gridView.setOnItemClickListener(new ItemClickListener());
  43.  
  44. popupWindow = new PopupWindow(contentView,
  45. ViewGroup.LayoutParams.MATCH_PARENT,
  46. ViewGroup.LayoutParams.WRAP_CONTENT);
  47.  
  48. popupWindow.setFocusable(true);// 取得焦点
  49. // 点击空白的地方关闭PopupWindow
  50. popupWindow.setBackgroundDrawable(new BitmapDrawable());
  51. //给popupWindow窗口加入动画效果
  52. popupWindow.setAnimationStyle(R.style.animation);
  53. parent = this.findViewById(R.id.main);
  54. }
  55.  
  56. private final class ItemClickListener implements OnItemClickListener {
  57. public void onItemClick(AdapterView<?> parent, View view, int position,
  58. long id) {
  59. if (popupWindow.isShowing())
  60. popupWindow.dismiss();// 关闭
  61. // ....
  62. }
  63. }
  64.  
  65. //将图片和名称字段对应到视图界面grid_view的图像和文本上
  66. private ListAdapter getAdapter() {
  67. List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
  68. for (int i = 0; i < images.length; i++) {
  69. HashMap<String, Object> item = new HashMap<String, Object>();
  70. item.put("image", images[i]);
  71. item.put("name", names[i]);
  72. data.add(item);
  73. }
  74. SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,
  75. R.layout.grid_item, new String[] { "image", "name" },
  76. new int[] { R.id.imageView, R.id.textView });
  77. return simpleAdapter;
  78.  
  79. }
  80. //打开PopupWindow窗口
  81. public void openPopWindow(View v) {
  82. //在父窗口的底部显示,没有偏移量
  83. popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
  84. }
  85.  
  86. @Override
  87. public boolean onCreateOptionsMenu(Menu menu) {
  88. getMenuInflater().inflate(R.menu.main, menu);
  89. return true;
  90. }
  91.  
  92. }

相对某个view的位置显示:

  1. lv_app_manager.setOnItemClickListener(new OnItemClickListener() {
  2.  
  3. public void onItemClick(AdapterView<?> parent, View view,
  4. int position, long id) {
  5.  
  6. // 获取当前view对象在窗体中的位置
  7. int[] arrayOfInt = new int[2];
  8. view.getLocationInWindow(arrayOfInt);
  9.  
  10. int i = arrayOfInt[0] + 60;
  11. int j = arrayOfInt[1];
  12.  
  13. View popupview = View.inflate(AppManagerActivity.this, R.layout.popup_item, null);
  14. LinearLayout ll_start = (LinearLayout) popupview .findViewById(R.id.ll_start);
  15. LinearLayout ll_uninstall = (LinearLayout) popupview.findViewById(R.id.ll_uninstall);
  16. LinearLayout ll_share = (LinearLayout) popupview.findViewById(R.id.ll_share);
  17.  
  18. // 把当前条目在listview中的位置设置给view对象
  19. ll_share.setTag(position);
  20. ll_uninstall.setTag(position);
  21. ll_start.setTag(position);
  22.  
  23. ll_start.setOnClickListener(AppManagerActivity.this);
  24. ll_uninstall.setOnClickListener(AppManagerActivity.this);
  25. ll_share.setOnClickListener(AppManagerActivity.this);
  26.  
  27. LinearLayout ll = (LinearLayout) popupview.findViewById(R.id.ll_popup);
  28. ScaleAnimation sa = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
    sa.setDuration(200);
  29. localPopupWindow = new PopupWindow(popupview, 230, 70);
  30. // 一定要记得给popupwindow设置背景颜色
  31. // Drawable background = new ColorDrawable(Color.TRANSPARENT);
  32. Drawable background = getResources().getDrawable( R.drawable.local_popup_bg);
  33. localPopupWindow.setBackgroundDrawable(background);
  34. localPopupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, i, j);
  35. ll.startAnimation(sa);
  36.  
  37. }
  38. });
  1. private void showPopUp(View v) {
  2. LinearLayout layout = new LinearLayout(this);
  3. layout.setBackgroundColor(Color.GRAY);
  4. TextView tv = new TextView(this);
  5. tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  6. tv.setText("I'm a pop -----------------------------!");
  7. tv.setTextColor(Color.WHITE);
  8. layout.addView(tv);
  9.  
  10. popupWindow = new PopupWindow(layout,120,120);
  11.  
  12. popupWindow.setFocusable(true);
  13. popupWindow.setOutsideTouchable(true);
  14. popupWindow.setBackgroundDrawable(new BitmapDrawable());
  15.  
  16. int[] location = new int[2];
  17. v.getLocationOnScreen(location);
  18.  
  19. popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());
  20. }
  1. pop = new PopupWindow(
  2. layout,
  3. myButton.getWidth(),
  4. myButton.getHeight() * 3 + 5);
  5.  
  6. ColorDrawable cd = new ColorDrawable(-0000);
  7. pop.setBackgroundDrawable(cd);
  8. // pop.showAsDropDown(v);
  9.  
  10. pop.update();
  11. pop.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
  12. pop.setTouchable(true); // 设置popupwindow可点击
  13. pop.setOutsideTouchable(true); // 设置popupwindow外部可点击
  14. pop.setFocusable(true); //获取焦点
  15. /*设置popupwindow的位置*/
  16. pop.showAtLocation(layout,
  17. (Gravity.BOTTOM - myButton.getHeight())
  18. | Gravity.LEFT, 0, 2 * myButton.getHeight());
  19. state = 1;
  20. pop.setTouchInterceptor(new View.OnTouchListener() {
  21. @Override
  22. public boolean onTouch(View v, MotionEvent event) {
  23. /**** 如果点击了popupwindow的外部,popupwindow也会消失 ****/
  24. if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
  25. pop.dismiss();
  26. return true;
  27. }
  28. return false;
  29. }
  30.  
  31. });

  由于手机分辨率不同,设置popwindow的宽度和高度时,如果给定数字,那么可能会在模拟器和手机上产生不同的效果。因此可以采用dip与px转换,也可以先获取手机的屏幕宽度和高度,根据宽度和高度进行设置。

  1. public class DensityUtil {
  2.  
  3. /**
  4. * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
  5. */
  6. public static int dip2px(Context context, float dpValue) {
  7. final float scale = context.getResources().getDisplayMetrics().density;
  8. return (int) (dpValue * scale + 0.5f);
  9. }
  10.  
  11. /**
  12. * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
  13. */
  14. public static int px2dip(Context context, float pxValue) {
  15. final float scale = context.getResources().getDisplayMetrics().density;
  16. return (int) (pxValue / scale + 0.5f);
  17. }
  18. }
  1. DisplayMetrics metrics = new DisplayMetrics();
  2. getWindowManager().getDefaultDisplay().getMetrics(metrics);
  3.  
  4. // mPopupWindow = new PopupWindow(popupLayout,metrics.widthPixels/3,+metrics.heightPixels/2);//采用手机宽度和高度进行设置
  5. mPopupWindow = new PopupWindow(popupLayout,DensityUtil.dip2px(this, ), DensityUtil.dip2px(this, ));//通过类进行转换
  1. 半透明弹出框PopUpWindow
  2. http://www.apkbus.com/android-2474-1-1.html
  3. PopUpWindow实现半透明弹出框关键点:
  4. 布局文件 最外层设置为全屏 背景颜色为半透明
  5. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  6. android:layout_width="fill_parent"
  7. android:layout_height="fill_parent"
  8. android:background="@color/translucent"
  9. android:gravity="center"
  10. android:orientation="vertical"
  11. >
  12. .......
  13. <ListView
  14. android:id="@+id/qianghaoqi_diary_pop_list"
  15. android:divider="@null"
  16. android:scrollingCache="false"
  17. android:fadingEdge="none"
  18. android:scrollbarThumbVertical="@drawable/game_blade_qianghaoqi_listview_scrollbar"
  19. android:layout_marginBottom="@dimen/dip5"
  20. android:layout_marginLeft="@dimen/dip5"
  21. android:layout_marginRight="@dimen/dip10"
  22. android:layout_width="fill_parent"
  23. android:layout_height="wrap_content"
  24. android:layout_gravity="center"
  25. android:listSelector="@null"
  26. android:paddingLeft="15.0dip"
  27. />
  28. .........
  29. </RelativeLayout>
  30. new一个全屏的PopUpWindow
  31. //必须为true,可以获取焦点
  32. mPopWin = new PopupWindow(aPopView, LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,true);
  33. //mPopWin.setHeight(450);//设置PopupWindow高度
  34. //必须设置。改变弹出窗口的背景,当然也可以设置为NULL。
  35. mPopWin.setBackgroundDrawable(mActivity.getResources().getDrawable(R.drawable.game_blade_qianghaoqi_transparent_bg));
  36. mPopWin.showAtLocation(aParentView,Gravity.CENTER, 0, 0);
  37. //如果窗口已经显示过,更改此值只能在下一次显示时起作用,或者调用update()
  38. mPopWin.update();
  39. 如果PopUpWindow内的布局会夺取焦点(如示例ListView),注意代码
  40. mListView.requestFocus();
  41. // 焦点到了listView上,所以需要监听此处的键盘事件。否则会出现不响应键盘事件的情况
  42. mListView.setOnKeyListener(new OnKeyListener() {
  43. @Override
  44. public boolean onKey(View v, int keyCode, KeyEvent event) {
  45. if (keyCode == KeyEvent.KEYCODE_BACK) {
  46. closePopWin();
  47. }
  48. return true;
  49. }
  50. });
  51. 关闭PopUpWindow
  52. private void closePopWin(){
  53. if (mPopWin != null && mPopWin.isShowing()) {
  54. mPopWin.dismiss();
  55. }
  56. }
  57. PopUpWindow一般应用
  58. mPopWin = new PopupWindow(mPopView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
  59. mPopWin.setBackgroundDrawable(mActivity.getResources().getDrawable(R.drawable.game_blade_qianghaoqi_transparent_bg));
  60. mPopWin.setOutsideTouchable(true);
  61. mPopWin.setFocusable(true);
  62. mPopWin.showAsDropDown(aParentView,0,-15);
  63. mPopWin.update();
  64.  
  65. 0 热度|全文链接
  66. #PopWin#半透明
  67.  
  68. AndroidPopWindow
  69.  
  70. 1.设置半透明主题
  71. 2.设置windowalpha
  72. // WindowManager.LayoutParams lp =getWindow().getAttributes();
  73. // lp.alpha =0.5f; //0.0-1.0
  74. // getWindow().setAttributes(lp);
  75.  
  76. 发现这两种都不能满足要求,起码的颜色就不太对。想做好点,做成类似alertDialog的样子,带边框,弹出窗口带动画效果,之后背景置灰,那多帅。
  77. 看到那个仿uc浏览器的源码,是用alertdialog做的,达到那种效果,加点动画就行了。下图是从那个ucweb源码里面弄出来的。
  78.  
  79. 上面的代码就不贴了,我上传的项目文件里面也有。
  80. 下面是弹出popupwindow的图片,第一张是动画中,第二张是完全弹出的:
  81.  
  82. 弹出popwindow的代码如下,比较乱,多包涵:
  83.  
  84. popupWindow = new PopupWindow(menuView,LayoutParams.FILL_PARENT,
  85. LayoutParams.FILL_PARENT,true);
  86. popupWindow.showAtLocation(findViewById(R.id.parent),Gravity.CENTER
  87. |Gravity.CENTER, 0, 0);
  88. popupWindow.setAnimationStyle(R.style.PopupAnimation);
  89. //加上下面两行可以用back键关闭popupwindow,否则必须调用dismiss();
  90. ColorDrawable dw = new ColorDrawable(-00000);
  91. popupWindow.setBackgroundDrawable(dw);
  92. popupWindow.update();
  93.  
  94. 下面是实现步骤:
  95. 1。背景置灰:
  96. popupWindow =new PopupWindow(menuView,LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT, true);
  97. 第二三个参数必须是LayoutParams.FILL_PARENT,这样才能填充整个屏幕,达到背景置灰的目的。
  98. 整个popupwindow里面是一个GridView,图片什么的也是用的那个仿UC浏览器界面项目的,在此谢谢了。
  99. 关键的东西都在xml里面。
  100.  
  101. <?xml version="1.0"encoding="utf-8"?>
  102. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  103. android:orientation="vertical"android:layout_width="fill_parent"
  104. android:gravity="center"android:layout_height="fill_parent"
  105. android:layout_gravity="center"android:background="#b0000000" >
  106. <LinearLayoutandroid:orientation="vertical"
  107. android:layout_width="wrap_content"android:gravity="center"
  108. android:layout_height="wrap_content"android:layout_gravity="center"
  109. android:background="@drawable/downbutton_corner">
  110. <GridViewandroid:id="@+id/gridview"android:layout_width="wrap_content"
  111. android:layout_height="wrap_content"android:numColumns="4"
  112. android:verticalSpacing="5dip"android:horizontalSpacing="5dip"
  113. android:stretchMode="columnWidth"android:gravity="center"
  114. android:layout_gravity="center"/></LinearLayout></LinearLayout>
  115.  
  116. 第一个linearlayout里面的android:background="#b0000000",就是全屏背景,网上搜的好多半透明都是“#e0000000”,我觉得那颜色太深,“#b0000000”更合适。
  117. 第二个linearlayoutpopupwind的背景,里面的android:background="@drawable/downbutton_corner"是关键,边框,圆角都是里面定义的。
  118.  
  119. 2popupwindow的边框,圆角背景。downbutton_corne.xml
  120. <shapexmlns:android="http://schemas.android.com/apk/res/android"
  121. android:shape="rectangle">
  122. <gradientandroid:startColor="#c0000000"android:endColor="#c0000000"
  123. android:angle="90" /><!--背景颜色渐变-->
  124. <strokeandroid:dashWidth="2dp" android:dashGap="2dp"
  125. android:width="2dp"android:color="#FF00ff00"></stroke>
  126. <!--描边-->
  127. <cornersandroid:bottomRightRadius="5dp"
  128. android:bottomLeftRadius="5dp"android:topLeftRadius="5dp"
  129. android:topRightRadius="5dp"/><!--设置圆角-->
  130. </shape>
  131.  
  132. 这个涉及到shape画图,要是不懂的话。网上很多资料,搜一下就是了。我博客里面也有,http://blog.csdn.net/ymdcr/archive/2010/12/01/6048256.aspx
  133. <gradient android:startColor="#c0000000"android:endColor="#c0000000" android:angle="90"/><!--背景颜色渐变 -->
  134. 我就设置了一个固定的颜色"#c0000000"android:angle="90"这个是设置颜色渐变方向,从上到下啊,从左到右啊,貌似只能90的倍数,也只有四个方向嘛。
  135. <stroke></stroke>,边框就是这个实现的。
  136. dashWidth指的是边线的宽度 dashGap 指的是每条线之间的间距,(因为是边线是很多小横线组成的)。
  137.  
  138. 3。淡入淡出动画
  139. popupWindow.setAnimationStyle(R.style.PopupAnimation);
  140. 这条代码是设置style的,动画文件就是在style文件里面引入的。下面是淡入的动画,动画教程网上也很多。淡出的动画就这些参数值交换位置就是了。android:duration这个是持续时间,为了截图,我把它弄成5秒了。
  141. <setxmlns:android="http://schemas.android.com/apk/res/android">
  142. <scaleandroid:fromXScale="0.6" android:toXScale="1.0"
  143. android:fromYScale="0.6" android:toYScale="1.0"android:pivotX="50%"
  144. android:pivotY="50%" android:duration="5000"/>
  145. <alphaandroid:interpolator="@android:anim/decelerate_interpolator"
  146. android:fromAlpha="0.0" android:toAlpha="1.0"android:duration="5000" />
  147. </set>
  148. 复制代码
  149.  
  150. 大概就是这些了。
  151.  
  152. 还有一个关键的问题。弹出pop之后,back键无效了,必须在pop里面设置事件dismiss掉。下面是问题的描述,哪位解决了,告诉我一下,谢谢。我的邮箱:
  153. 问题解决了,是因为没设置背景的原因。
  154. popupWindow.setBackgroundDrawable(new BitmapDrawable());
  155. //把这一行放在showAtLocation前面就行了,以前是放在后面的,粗心了。
  156. popupWindow.showAtLocation(findViewById(R.id.parent),Gravity.CENTER
  157. |Gravity.CENTER, 0,0);
  158. 网上也有很多人说,弹出pop之后,不响应键盘事件了,这个其实是焦点在pop里面的view去了。
  159. 以这个为例,焦点就在gridview上面去了。28楼的兄弟提示的,谢了。
  160. gridview加上setOnKeyListener,就能解决。
  161. menuGrid.setOnKeyListener(newOnKeyListener() {
  162. @Override
  163. publicboolean onKey(View v, int keyCode, KeyEvent event){
  164. switch(keyCode) {
  165. caseKeyEvent.KEYCODE_MENU:
  166. if(popupWindow != null &&popupwindows.isShowing()) {
  167. popupWindow.dismiss();
  168. System.out.println("menuGridfdsfdsfdfd");
  169. }
  170. break;
  171. }
  172.  
  173. returntrue;
  174. }
  175. });
  176.  
  177. ---------------------------------------------------------------------------------
  178. 使用PopupWindow来做自定义menu,往PopupWindow增加一个子View,子View的布局就是menu的布局。
  179. 出现和退出的动画:可以给PopUpWindow或它的子view添加。
  180. 网上所有用PopupWindow做的menu有个共同特点:就是点击menu键出现PopupWindow,然后再点击menu键无法使PopupWindow退出/dismiss()。
  181. 当给PopupWindow设置了setFocusable(true),menu显示后,点击menu其他任何地方,menu都会消失,但是这时候按钮的点击事件其实是不响应的。同时只响应键盘的返回键,其他按键均不响应,比如点击menu键,没有任何反应。
  182. 要解决这个问题很简单,就是给PopupWindow的子View设置下面的代码:
  183. [java] view plaincopy
  184. //sub_view 是PopupWindow的子View
  185. sub_view.setFocusableInTouchMode(true);
  186. sub_view.setOnKeyListener(new OnKeyListener() {
  187. @Override
  188. public boolean onKey(View v, int keyCode, KeyEvent event) {
  189. // TODO Auto-generated method stub
  190. if ((keyCode == KeyEvent.KEYCODE_MENU)&&(mPopupWindow.isShowing())) {
  191. mPopupWindow.dismiss();// 这里写明模拟menu的PopupWindow退出就行
  192. return true;
  193. }
  194. return false;
  195. }
  196. });
  197.  
  198. 记住,一定要给PopupWindow设置setFocusable(true),要不然点击menu其他地方以及返回键,menu都不会退出。且这时候是响应PopupWindowparentmenu事件的。
  199. 下面阐述为什么这么写之后,当PopupWindow显示后,点击menuPopupWindow会退出的原因:
  200. 首先得明白为什么给PopupWindow setFocusable(true)后,点击menu出现PopupWindow后再点击menu没反应的原因。
  201. PopupWindow初始化的时候一般都指定了在哪个View上出现,我们称这个Viewparentparent里面写了点击menu出现PopupWindow的事件,如果给PopupWindow setFocusable(true),此时屏幕的焦点在PopupWindow上面,肯定是不会响应parent的按键事件的,它只会响应PopupWindow的按键事件。
  202. 但是PopupWindow的本质是Window,没有继承View类,自己没有onkeyDownonkeydispatchKey这些事件的。我刚开始试着实现这些接口,但是按键依然不响应,不知原因。因现在对按键的原理还不熟,无法阐述其原因。
  203. 然后我想绕道而行,就是给PopupWindow的子View注册按键事件,setKeyListener,刚开始我在子Viewxml设置了android:focusable=”true 但按键事件依然不响应。。。。纠结啊纠结。。。然后没得办法,我google了所有关于PopupWindow的文章。。。最后终于被我发现。。。需要给PopupWindow的子View 设置setFocusableInTouchMode(true)。这时候按键事件就响应了。。。
  204.  
  205. 下面附上完整代码:
  206. [java] view plaincopy
  207. /*必须重写,否则点击MENU无反应 为了让他不显示,下面onMenuOpened()必须返回false*/
  208. @Override
  209. public boolean onCreateOptionsMenu(Menu menu) {
  210. menu.add("menu");// 必须创建一项
  211. return super.onCreateOptionsMenu(menu);
  212. }
  213. /**
  214. * 拦截MENU
  215. */
  216. @Override
  217. public boolean onMenuOpened(int featureId, Menu menu) {
  218. if(mPopupWindow != null){
  219. if(!mPopupWindow.isShowing()){
  220. /*最重要的一步:弹出显示 在指定的位置(parent) 最后两个参数 是相对于 x / y 轴的坐标*/
  221. mPopupWindow.showAtLocation(findViewById(R.id.linear_menu_parent), Gravity.BOTTOM, 0, 0);
  222. }
  223. }
  224. return false;// 返回为true 则显示系统menu
  225. }
  226.  
  227. private void initPopuWindow(int menuViewID){
  228. LayoutInflater mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
  229. /*设置显示menu布局 view子VIEW*/
  230. sub_view = mLayoutInflater.inflate(menuViewID, null);
  231. /*第一个参数弹出显示view 后两个是窗口大小*/
  232. mPopupWindow = new PopupWindow(sub_view, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
  233. /*设置背景显示*/
  234. mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_menu_popup));
  235. /*设置触摸外面时消失*/
  236. mPopupWindow.setOutsideTouchable(true);
  237. /*设置系统动画*/
  238. mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
  239. mPopupWindow.update();
  240. mPopupWindow.setTouchable(true);
  241. /*设置点击menu以外其他地方以及返回键退出*/
  242. mPopupWindow.setFocusable(true);
  243.  
  244. /** 1.解决再次点击MENU键无反应问题
  245. * 2.sub_view是PopupWindow的子View
  246. */
  247. sub_view.setFocusableInTouchMode(true);
  248. sub_view.setOnKeyListener(new OnKeyListener() {
  249. @Override
  250. public boolean onKey(View v, int keyCode, KeyEvent event) {
  251. // TODO Auto-generated method stub
  252. if ((keyCode == KeyEvent.KEYCODE_MENU)&&(mPopupWindow.isShowing())) {
  253. mPopupWindow.dismiss();// 这里写明模拟menu的PopupWindow退出就行
  254. return true;
  255. }
  256. return false;
  257. }
  258. });
  259.  
  260. /*监听MENU事件*/
  261. menu = new View[3];
  262. menu[0] = sub_view.findViewById(R.id.menu_0);
  263. menu[1] = sub_view.findViewById(R.id.menu_1);
  264. menu[2] = sub_view.findViewById(R.id.menu_2);
  265.  
  266. menu[0].setOnClickListener(new OnClickListener() {
  267. @Override
  268. public void onClick(View v) {
  269. // doSomething
  270.  
  271. }
  272. });
  273.  
  274. menu[1].setOnClickListener(new OnClickListener() {
  275. @Override
  276. public void onClick(View v) {
  277. // doSomething
  278.  
  279. }
  280. });
  281.  
  282. menu[2].setOnClickListener(new OnClickListener() {
  283. @Override
  284. public void onClick(View v) {
  285. // doSomething
  286.  
  287. }
  288. });

Android学习笔记_35_PopupWindow泡泡窗口的实现及GridView应用的更多相关文章

  1. android学习笔记---63-PopupWindow,泡泡窗口的实现

    转载http://blog.csdn.net/lidew521/article/details/8976627 PopupWindow是一个可以显示在当前Activity之上的浮动容器,PopupWi ...

  2. Android学习笔记之Activity详解

    1 理解Activity Activity就是一个包含应用程序界面的窗口,是Android四大组件之一.一个应用程序可以包含零个或多个Activity.一个Activity的生命周期是指从屏幕上显示那 ...

  3. 【转】Pro Android学习笔记(十二):了解Intent(下)

    解析Intent,寻找匹配Activity 如果给出component名字(包名.类名)是explicit intent,否则是implicit intent.对于explicit intent,关键 ...

  4. 【转】Pro Android学习笔记(二):开发环境:基础概念、连接真实设备、生命周期

    在Android学习笔记(二):安装环境中已经有相应的内容.看看何为新.这是在source网站上的Android架构图,和标准图没有区别,只是这张图颜色好看多了,录之.本笔记主要讲述Android开发 ...

  5. Android 学习笔记之Volley(七)实现Json数据加载和解析...

    学习内容: 1.使用Volley实现异步加载Json数据...   Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...

  6. Android学习笔记进阶之在图片上涂鸦(能清屏)

    Android学习笔记进阶之在图片上涂鸦(能清屏) 2013-11-19 10:52 117人阅读 评论(0) 收藏 举报 HandWritingActivity.java package xiaos ...

  7. android学习笔记36——使用原始XML文件

    XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...

  8. Android学习笔记之JSON数据解析

    转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...

  9. udacity android 学习笔记: lesson 4 part b

    udacity android 学习笔记: lesson 4 part b 作者:干货店打杂的 /titer1 /Archimedes 出处:https://code.csdn.net/titer1 ...

随机推荐

  1. 【python数据分析】利用Anaconda在window上搭建数据分析环境

    由于在进行数据分析过程中,需要安装一些第三方库,导致python总会报一些错误,现将通过利用Anaconda搭建数据分析环境,已测可用. 1.到官网上下载python:https://www.pyth ...

  2. 数据库SQL优化百万级数据库优化方案

    1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  3. python 模板注入

    今天学习了python的模板注入,这里自己搭建环境测试以下,参考文章:http://www.freebuf.com/articles/web/136118.html web 程序包括两个文件: fla ...

  4. React之特点及常见用法

    1.什么是React? React是一个用于构建用户界面的JavaScript库.主要用于构建UI,很多人认为Reatc是MVC中的V(视图). React起源于Facebook的内部项目,用来架构I ...

  5. [转]ASP.NET MVC 5 List Editor with Bootstrap Modals

    本文转自:https://www.codeproject.com/articles/786085/asp-net-mvc-list-editor-with-bootstrap-modals With ...

  6. 通过js控制层的动态隐藏

    <style type="text/css"> #dv1{ width:1000px; height:1000px; overflow:hidden; display: ...

  7. Scala 知识点掌握2

    Scala 基础知识点巩固2 1.集合中常用的函数 sum / max / min # 定义一个List[Int]val list1 = List(1,3,4,6,8,9)# 取集合中所有元素的和li ...

  8. 线程的Interrupt方法与InterruptedException解析

    线程阻塞状态与等待状态(当一个线程处于被阻塞或等待状态时,它暂时不活动,不允许任何代码且消耗最少的资源) 当一个线程试图获得一个内部的对象锁(而不是java.util.concurrent库中的锁), ...

  9. Java使用TCP聊天程序

    前面使用了UDP进行通信的聊天程序 现在做一个用TCP进行通信的聊天程序 原理: ServerSocket Socket 1.开一个线程监听端口,准备接收消息 2.不断接受消息发送到目的端口 P.S. ...

  10. Web前端面试指导(七):入职后的建议

    7.2 关于合同 签合同的时候,看公司要求,有些是3年,有些是5年,不要怕,签了就是了,真到想走的时候,提前说一声,随时可以走,不存在什么违约赔偿. 注意:你的合同和薪资都是属于保密的,不能让公司其他 ...