您的位置:首页 > 移动开发 > Android开发

android 之popupWindow 在指定位置上的显示

2016-05-27 22:09 639 查看
http://blog.csdn.net/dxj007/article/details/8026691

标签: androidlayout
2012-09-27 22:27 101379人阅读 评论(24) 收藏 举报


 分类:
 

android开发(4) 


版权声明:本文为博主原创文章,未经博主允许不得转载。

这篇文章主要介绍了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]);  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: