2016-03-14 81 views
0

我想在textview上的特定位置播放彈出窗口(textview位於scrollview內)。我的PopupWindow佈局只包含一個列表視圖。我使用下面的代碼來調整PopupWindow寬度和高度。 PopupWindow高度正確,但寬度不正確。它在不同的設備上顯示不同。它也不會扭曲列表視圖內容的寬度。如何在Android中調整PopupWindow寬度

final PopupWindow attachmentPopup = new PopupWindow(this); 
LayoutInflater layoutInflater = getLayoutInflater(); 
View popupView = layoutInflater.inflate(R.layout.popupwindow, null); 
ListView lv = (ListView) popupView.findViewById(R.id.listView); 
final ArrayList<CustomItem> popupItems = new ArrayList<CustomItem>(); 
final CustomAdapter popupAdapter = new CustomAdapter(popupItems, Details.this); 
CustomItem item; 
item = new CustomItem(); 
item.setName("Apple"); 
popupItems.add(item); 
item = new CustomItem(); 
item.setName("Orange");   
popupItems.add(item); 
lv.setAdapter(popupAdapter); 
int popupWidth = ViewGroup.LayoutParams.WRAP_CONTENT; 
int popupHeight = ViewGroup.LayoutParams.WRAP_CONTENT; 
attachmentPopup.setFocusable(true); 
attachmentPopup.setWidth(popupWidth); 
attachmentPopup.setHeight(popupHeight); 
attachmentPopup.setContentView(popupView); 
attachmentPopup.setBackgroundDrawable(new BitmapDrawable()); 
attachmentPopup.showAtLocation(popupView, Gravity.TOP | Gravity.LEFT, mx, my); 

popupwindow.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:background="#9acd32" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/listView" /> 

</LinearLayout> 

回答

0

能否請您試試這個

int popupWidth = ViewGroup.LayoutParams.WRAP_CONTENT; 
int popupHeight = ViewGroup.LayoutParams.WRAP_CONTENT; 

int popupWidth = WindowManager.LayoutParams.WRAP_CONTENT; 
int popupHeight = WindowManager.LayoutParams.WRAP_CONTENT; 
+0

的結果是相同的替換。我認爲,因爲listview它沒有正確測量listview項目的寬度。 – user934820

+0

@ user934820我不知道,但你可以請嘗試設置android:orientation =「vertical」在彈出視圖的線性佈局 – dex

+1

你會介意共享整個代碼,以便我可以編譯在我的最後? – dex