2013-04-07 12 views
11

मैं पहले से ही पॉप अप विंडो की स्थापना हो जाओ, लेकिन मैं बटन (v देखें), जरूरत है कि नीचे यह केंद्र के लिए चाहते हैं इसे खोलने के लिए क्लिक किए जाने की:पॉपअप विंडो के उपाय

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); 
} 

किसी को भी है उपायों को पाने का विचार?

उत्तर

21

आपको दृश्य की ऊंचाई या चौड़ाई नहीं मिलेगी, जो स्क्रीन पर नहीं खींची गई है।

pupLayout.getWidth() // इस आप 0

दे देंगे आप इस

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

तरह चौड़ाई इस

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); 
की तरह इसका इस्तेमाल प्राप्त करने की आवश्यकता
संबंधित मुद्दे