2012-03-22 39 views
2

我在使用PopupWindow在單擊ListView項目時顯示一些子類別。 我對ListView行(類別)和PopupWindow(類別)使用相同的佈局。PopupWindow的寬度/風格的問題

我的問題是,我將我的PopupWindow的寬度設置爲我的行項目視圖的寬度,但是當它顯示時,它有點窄,帶有某種邊框。

在下面我把PopupWindow上的ListView瑟的差別

爲什麼較小的頂部的屏幕截圖?我怎樣才能刪除這個邊界?

enter image description here

listView.setOnItemClickListener(...)

listView.setOnItemClickListener(new OnItemClickListener() { 

    @Override 
    public void onItemClick(AdapterView<?> a, View v, int position, long id) {   

      showPopup(HomeActivity.this, layout_base, v, position); 
     } 
    } 
}); 

showPopup(...)

public void showPopup(Context context, View parent, View view, int position){ 

    /* 
    * Compute popup position and size 
    */ 

    int[] itemPosition = new int[2]; 
    view.getLocationOnScreen(itemPosition); 

    int item_width = view.getWidth(); 
    int item_height = view.getHeight(); 

    int nChildren = GlobalVars.menuItemArray[position].children.size(); 

    int popup_height = item_height * nChildren; 

    /* 
    * Build the view 
    */ 

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    LinearLayout popupContainer = new LinearLayout(context); 
    popupContainer.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    popupContainer.setOrientation(LinearLayout.VERTICAL); 

    LayoutParams params; 

    params = new LayoutParams(item_width, item_height); 
    popupContainer.setOrientation(LinearLayout.VERTICAL); 

    for(int i=0; i<nChildren; i++){ 

     RelativeLayout menu_row = (RelativeLayout) inflater.inflate(R.layout.menu_row, null);   

     popupContainer.addView(menu_row, params); 
    } 

    mPopup.setContentView(popupContainer); 
    mPopup.setHeight(popup_height); 
    mPopup.setWidth(item_width); 

    mPopup.showAtLocation(parent, Gravity.TOP|Gravity.LEFT, 0, 0);   

} 

回答

7

我不得不刪除PopupWindow的背景使用:

mPopup.setBackgroundDrawable(new BitmapDrawable()); 
+2

你可以使用「null」作爲參數,爲你節省一些內存。 – manmal 2012-11-17 16:14:32

+0

我希望我能不止一次地對此讚不絕口。 Android充滿了怪癖和錯誤我花了更多時間來對抗API,而不是真正的編碼。 – 2013-07-23 22:16:29

+1

@manmal當我將它設置爲null時,當我點擊它之外時,PopupWindow變得無法解散。不知道爲什麼。 (如果有人遇到這個問題) – 2013-07-23 22:17:49