2016-05-31 86 views
0

我有一個由佈局定製的彈出窗口。在點擊a_btn之後,我必須給出一個x,y座標以顯示彈出窗口。這可能是不同手機的不同位置。在點擊按鈕附近對齊彈出窗口

但是我想表明總是高於彈出窗口和觸摸的a_btn

我如何能實現this.Help我

我對顯示彈出窗口代碼:

a_btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      LayoutInflater lInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View popup_view = lInflater.inflate(R.layout.popup_a, null); 
      final PopupWindow popup = new PopupWindow(popup_view,FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT,true); 
      popup.setFocusable(true); 
      popup.setBackgroundDrawable(new ColorDrawable()); 
      popup.showAtLocation(relative, Gravity.NO_GRAVITY, coordinateTop, 100); 
      //popup.showAsDropDown(location_popup_view, 2, 2); 


     } 
    }); 

回答

1

我用popup.showAsDropDown(a_btn,0,0);

,而不是 popup.showAtLocation(relative, Gravity.NO_GRAVITY, coordinateTop, 100);

,給XOFF和YOFF。

a_btn.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) {  
LayoutInflater lInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View popup_view = lInflater.inflate(R.layout.popup_a, null); 
     final PopupWindow popup = new PopupWindow(popup_view,200,200,true); 
     popup.setFocusable(true); 
     popup.setBackgroundDrawable(new ColorDrawable()); 
     popup.showAsDropDown(a_btn,0,0); 


    } 
}); 
+0

我想你還需要包括設備的dpi級別,然後乘以你想要的位置 –

1

使用此代碼將幫助您

a_btn.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Rect r = locateView(v);  
    LayoutInflater lInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View popup_view = lInflater.inflate(R.layout.popup_a, null); 
      final PopupWindow popup = new PopupWindow(popup_view,FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT,true); 
      popup.setFocusable(true); 
      popup.setBackgroundDrawable(new ColorDrawable()); 
      popup.showAtLocation(layout, Gravity.TOP | Gravity.LEFT, r.right, r.bottom); 


     } 
    }); 



    public static Rect locateView(View v) { 
      int[] loc_int = new int[2]; 
      if (v == null) 
       return null; 
      try { 
       v.getLocationOnScreen(loc_int); 
      } catch (NullPointerException npe) { 
       return null; 
      } 
      Rect location = new Rect(); 
      location.left = loc_int[0]; 
      location.top = loc_int[1]; 
      location.right = loc_int[0] + v.getWidth(); 
      location.bottom = loc_int[1] + v.getHeight(); 
      return location; 
     } 
相關問題