PopupWindow的使用和分析 弹出框的一种
2016-03-09 14:28
459 查看
PopupWindow使用
PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。自己的例子:
/** * 创建PopupWindow */ private PopupWindow popupWindow; private LinearLayout popupWindow_view; protected void initPopuptWindow() { // TODO Auto-generated method stub // 获取自定义布局文件activity_popupwindow_left.xml的视图 // popupWindow_view = (LinearLayout) getLayoutInflater().inflate(R.layout.welcome, null,false); popupWindow_view = new LinearLayout(context); popupWindow_view.setBackgroundResource(R.color.blackbantouming); // popupWindow_view.setBackgroundColor(R.color.red); // 创建PopupWindow实例,200,LayoutParams.MATCH_PARENT分别是宽度和高度LayoutParams.MATCH_PARENT popupWindow = new PopupWindow(popupWindow_view,LayoutParams.FILL_PARENT , LayoutParams.FILL_PARENT, true); // 这里是位置显示方式,在屏幕的左侧 popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); // 设置动画效果 // popupWindow.setAnimationStyle(R.style.AnimationFade); // 点击其他地方消失 popupWindow_view.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); popupWindow = null; } return false; } }); }
/*** * 获取PopupWindow实例 */ private void getPopupWindow() { if (null != popupWindow) { popupWindow.dismiss(); return; } else { initPopuptWindow(); } }
使用时:
/** * boolean sor 是否是排序(排序要放到下方) * LinearLayout sumLayout 存放的控件 final List<Datas> list 数据源 boolean box 下方需要不 boolean with 是否跟随子控件的宽度并居中 final FeelForeverService feel * */ public void OneLayoutList(boolean sor,View v , final List<Datas> list, boolean box,boolean with, final FeelForeverService feel){ getPopupWindow(); //这是指定在哪个控件之下 if(sor){ int[] location = new int[2]; v.getLocationOnScreen(location); popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight()); }else { popupWindow.showAsDropDown(v); } popupWindow_view.removeAllViews(); LinearLayout layoutTitle=new LinearLayout(context); layoutTitle.setOrientation(LinearLayout.HORIZONTAL); LayoutInflater inflaterTitle = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); inflaterTitle.inflate(R.layout.service_onelist, layoutTitle); ListView list1 = (ListView) layoutTitle.findViewById(R.id.service_list_one); LinearLayout service_box_001= (LinearLayout) layoutTitle.findViewById(R.id.service_box_001); if(box) service_box_001.setVisibility(View.VISIBLE); else service_box_001.setVisibility(View.GONE); Button box_but_OK = (Button) layoutTitle.findViewById(R.id.box_but_OK_02); final EditText box_min = (EditText) layoutTitle.findViewById(R.id.box_min_02); final EditText box_max = (EditText) layoutTitle.findViewById(R.id.box_max_02); box_but_OK.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { feel.getOncliekOne("",box_min.getText().toString().trim() +"-"+ box_max.getText().toString().trim(), (box_min.getText().toString().trim().equals("")?"0":box_min.getText ().toString().trim()) + "-"+ (box_max.getText().toString().trim().equals ("")?"9999999":box_max.getText().toString().trim())); popupWindow.dismiss(); } }); RelativeLayout onelistBox =(RelativeLayout) layoutTitle.findViewById(R.id.onelistBox); ViewGroup.LayoutParams params = onelistBox.getLayoutParams(); params.height = SinyiApplication.WindowsHeight/2; onelistBox.setLayoutParams(params); list1.setAdapter(new ServiceAdapter(list)); list1.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { popupWindow.dismiss(); feel.getOncliekOne("" , list.get(arg2).getName(),list.get(arg2).getNote()); } }); //这里处理 popupWindow_view.addView(layoutTitle); //如果是排序,就把list置底 if(sor){ popupWindow_view.setGravity(Gravity.BOTTOM); }else{ popupWindow_view.setGravity(Gravity.TOP); } if(with){ setImageWanH(layoutTitle, v.getWidth()); popupWindow_view.setGravity(Gravity.CENTER_HORIZONTAL); } }
一些查找的资料:
介绍了popupWindow 在控件的各个方向上的显示(上、下、左、右),主要用到popupWindow 的showAtLocation()方法:
在控件的上方:
[java] view
plain copy
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
plain copy
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);
右边:
[html] view
plain copy
popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);
PopupWindow显示的方法有三个,showAsDropDown(anchor),showAsDropDown(anchor,
xoff, yoff)和showAtLocation(parent, gravity, x, y)。
前两个showAsDropDown方法是让PopupWindow相对于某个控件显示,而showAtLocation是相对于整个窗口的。
第一个参数是View类型的parent,虽然这里参数名是parent,其实,不是把PopupWindow放到这个parent里,并不要求这个parent是一个ViewGroup,这个参数名让人误解。官方文档”a parent view to get the android.view.View.getWindowToken()
token from
“,这个parent的作用应该是调用其getWindowToken()方法获取窗口的Token,所以,只要是该窗口上的控件就可以了。
第二个参数是Gravity,可以使用|附加多个属性,如Gravity.LEFT|Gravity.BOTTOM。
第三四个参数是x,y偏移。
待补充。。。。。。
相关文章推荐
- opencv mat元素的遍历
- Vim基础知识
- Dedecms网站搬家
- linux中shell变量$#,$@,$0,$1,$2的含义解释
- log4j.properties配置详解与实例-全部测试通过
- 查看centos版本号
- centos6.5挂载ntfs
- 修复office 2007或2010安装程序找不到Proplas.ww/Proplsww.cab
- 【转】Xcode真机测试could not find developer disk image解决方法
- 构建高效安全的Nginx Web服务器
- linux命令: ps、grep、kill
- Linux安装php运行环境
- linux跨文件复制粘贴
- Hadoop-新增节点
- 微信Android客户端架构演进之路
- linux命令
- nginx优化 突破十万并发
- 实验三:跟踪分析Linux内核的启动过程
- 深入剖析tomcat 笔记——第8章 载入器
- linux c/c++ GDB教程详解