2013-04-07 4 views
11

ich bereits das Pop-up-Fenster einrichten, aber ich will es unter der Schaltfläche zum Zentrum (Blick v), die es zu öffnen geklickt werden muss, um:Holen Sie sich die Maßnahmen von Popup-Fenster

public void showPopup(Context c, View v){ 
    int[] location = new int[2]; 
    v.getLocationOnScreen(location); 

    ViewGroup base = (ViewGroup) getView().findViewById(R.id.pup_pattern); 
    LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View pupLayout = inflater.inflate(R.layout.linearlayout_popup, base); 

    final PopupWindow pup = new PopupWindow(pupLayout, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT); 

    int x = location[0] - (int) ((pupLayout.getWidth() - v.getWidth())/2); // --> pupLayout.getWidth() gives back -2? 
    int y = location[1] + v.getHeight() + 10; 

    pup.setFocusable(true); 
    pup.showAtLocation(v, Gravity.NO_GRAVITY, x, y); 
} 

Hat jemand eine Idee, um Maßnahmen zu bekommen?

Antwort

21

Sie erhalten nicht die Höhe oder Breite der Ansicht, die nicht auf dem Bildschirm gezeichnet wurde.

pupLayout.getWidth() // dies wird Ihnen 0

Sie müssen die Breite wie diese

int width = MeasureSpec.makeMeasureSpec(0, MeasureSpec. UNSPECIFIED); 

Verwenden Sie es wie folgt

View pupLayout = inflater.inflate(R.layout.linearlayout_popup, base); 
pupLayout.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
      MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 

int x = location[0] - (int) ((pupLayout.getMeasuredWidth() - v.getWidth())/2); 
erhalten