2014-10-02 87 views
1

我已經爲這個問題建立了很多解決方案,但是他們都沒有爲我工作。我想在片段中顯示一個PopupWindow。這是我的代碼PopupWindow沒有顯示

LayoutInflater layoutInflater = LayoutInflater.from(getActivity()); 
View popupView = layoutInflater.inflate(R.layout.pop_up_cargando, null,false); 
this.popupWindow = new PopupWindow(popupView, popupView.getWidth(),popupView.getHeight()); 
this.popupWindow.setFocusable(true); 
int location[] = new int[2]; 
this.btnInventario.getLocationOnScreen(location); 
this.popupWindow.showAtLocation(this.btnInventario, Gravity.NO_GRAVITY, location[0], location[1] + btnInventario.getHeight()); // this.btnInventario is the button that calls this code 
this.popupWindow.update(0, 0, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

但是PopupWindow從不出現。

編輯:這是pop_up_cargando

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/popup_recomendar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#ffffff" 
    android:orientation="vertical" > 
    <ProgressBar 
     style="?android:attr/progressBarStyleHorizontal" 
     android:layout_width="180dp" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:layout_marginTop="20dp" 
     android:layout_gravity="center" 
     android:id="@+id/cargando"/> 
    <TextView 
     android:id="@+id/txtTexto" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:layout_marginBottom="20dp" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:layout_gravity="center" 
     android:text="@string/vacio" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 
</LinearLayout> 

任何建議的內容?我做錯了什麼?您的幫助將非常感激。

+0

我使用的是popupWindow.showAtLocation。它在代碼 – martinc 2014-10-02 16:31:14

+0

檢查'popupView.getWidth()'和'popupView.getHeight()'值是否大於0. – 2014-10-02 16:59:51

+0

@ G.T。兩者都是0 ...沒有注意到。我該如何解決它? – martinc 2014-10-02 17:12:31

回答

1

您的popupView.getWidth()popupView.getHeight()值等於0,因爲該視圖尚未繪製。

之前要求的寬度高度你的觀點,你必須確保它已經繪就。對於您可以撥打下面的方法已經充氣之後:

popupView.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); 

之後,您的視圖的大小是可用的方法getMeasuredWidth()getMeasuredHeight()

+0

感謝您的幫助。添加了這些行。現在,它取得了正確的高度和寬度值,但彈出窗口仍然不會出現... – martinc 2014-10-02 17:42:01

+0

如果用'popupWindow.show()'替換最後兩行,該怎麼辦? – 2014-10-02 17:50:07

+0

不能:「方法show()未定義爲PopupWindow類型」 – martinc 2014-10-02 19:11:31