2012-03-22 60 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