zoukankan      html  css  js  c++  java
  • android 之popupWindow 在指定位置上的显示

    这篇文章主要介绍了popupWindow 在控件的各个方向上的显示(上、下、左、右),主要用到popupWindow 的showAtLocation()方法:

    在控件的上方:

    [java] view plaincopy
     
    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.     }  


    在控件的其他方向上显示只需修改最后一行代码即可,如:

    下方:popupWindow.showAsDropDown(v);

    左边:

    [java] view plaincopy
     
    1. popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-popupWindow.getWidth(), location[1]);  

    右边:

    [html] view plaincopy
     
      1. popupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0]+v.getWidth(), location[1]);  
  • 相关阅读:
    测试常用的sql语句总结
    测试常用的Linux命令总结
    【转载】vim 中如何替换选中行或指定几行内的文本
    1074 Reversing Linked List
    1077 Kuchiguse
    LC 355. Design Twitter
    LCP 5. 发 LeetCoin
    LC 1409. Queries on a Permutation With Key
    1095 Cars on Campus
    LC 1369. Get the Second Most Recent Activity
  • 原文地址:https://www.cnblogs.com/zhuzhengwen1983/p/3612667.html
Copyright © 2011-2022 走看看