android学习——popupWindow 在指定位置上的显示
2013-02-04 21:32
399 查看
先看效果图,免得浪费大家时间,看是不是想要的效果 。
直接上代码 ,核心方法。
popupWindow 在控件的各个方向上的显示(上、下、左、右),主要用到popupWindow 的showAtLocation()方法:
在控件的上方:
[java] view
plaincopy
private void showPopUp(View v) {
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundColor(Color.GRAY);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("I'm a pop -----------------------------!");
tv.setTextColor(Color.WHITE);
layout.addView(tv);
popupWindow = new PopupWindow(layout,120,120);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
int[] location = new int[2];
v.getLocationOnScreen(location);
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());
}
在控件的其他方向上显示只需修改最后一行代码即可,如:
下方:popupWindow.showAsDropDown(v);
左边:
[java] view
plaincopy
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);
右边:
[html] view
plaincopy
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);
直接上代码 ,核心方法。
private void showPopupWindow(View parent) { if (popupWindow == null) { LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = layoutInflater.inflate(R.layout.group_list, null); lv_group = (ListView) view.findViewById(R.id.lvGroup); Collections.reverse(groups); GroupAdapter groupAdapter = new GroupAdapter(this, groups); lv_group.setAdapter(groupAdapter); popupWindow = new PopupWindow(view, 200, 220); } popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景 popupWindow.setBackgroundDrawable(new BitmapDrawable()); WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE); int xPos = -popupWindow.getWidth() / 2 + getCustomTitle().getCenter().getWidth() / 2; popupWindow.showAsDropDown(parent, xPos, 4); lv_group.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { loadNew(((StringItem)(groups.get(position))).getId()); if (popupWindow != null) popupWindow.dismiss(); } }); }
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); int measuredWidth = view.getMeasuredWidth(); int measuredHeight = view.getMeasuredHeight(); if (popupWindow == null) { popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true); } popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); // 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景 //popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow.showAsDropDown(v,v.getWidth()+ Utils.dip2px(HomeWorkActivity.this, 8), -(measuredHeight/2 + v.getHeight()/2)); popupWindow.setTouchInterceptor(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_OUTSIDE){ popupWindow.dismiss(); return true; } return false; } }); popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { } });
popupWindow 在控件的各个方向上的显示(上、下、左、右),主要用到popupWindow 的showAtLocation()方法:
在控件的上方:
[java] view
plaincopy
private void showPopUp(View v) {
LinearLayout layout = new LinearLayout(this);
layout.setBackgroundColor(Color.GRAY);
TextView tv = new TextView(this);
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("I'm a pop -----------------------------!");
tv.setTextColor(Color.WHITE);
layout.addView(tv);
popupWindow = new PopupWindow(layout,120,120);
popupWindow.setFocusable(true);
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
int[] location = new int[2];
v.getLocationOnScreen(location);
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight());
}
在控件的其他方向上显示只需修改最后一行代码即可,如:
下方:popupWindow.showAsDropDown(v);
左边:
[java] view
plaincopy
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);
右边:
[html] view
plaincopy
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);
相关文章推荐
- android学习——popupWindow 在指定位置上的显示
- android学习——popupWindow 在指定位置上的显示
- android学习——popupWindow 在指定位置上的显示
- android学习——popupWindow 在指定位置上的显示
- android学习——popupWindow 在指定位置上的显示
- android学习——popupWindow 在指定位置上的显示
- android学习——popupWindow 在指定位置上的显示
- android 学习之popupWindow 在指定位置上的显示,进出样式。
- Android PopupWindow 弹框布局要显示在某个空间下面位置
- arcgis for android 学习 - (5) 在地图指定位置添加“标记“,并尝试选中它
- popupWindow 在指定位置上的显示
- 关于Android popupWindow的位置显示 showAtLocation理解
- Android PopupWindow用法(二)_showAsDropDown 方法显示的位置
- arcgis for android 学习 - (5) 在地图指定位置添加“标记“,并尝试选中它
- android 之popupWindow 在指定位置上的显示
- popupWindow 指定位置上的显示
- android 之popupWindow 在指定位置上的显示
- Android_百度地图API_通过点击地图在指定位置显示PopWindow
- Android,安卓,toolbar,menu显示位置,及样式调整,及使用PopupWindow 实现menu下拉菜单gong
- android popupwindow 位置显示