PopupWindow的宽度/样式问题

时间:2012-03-22 16:05:59

标签: android popupwindow

我在点击ListView项目时使用PopupWindow来显示一些子类别。 我对ListView行(类别)和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);         

}

1 个答案:

答案 0 :(得分:7)

我必须使用以下方法删除PopupWindow的背景:

mPopup.setBackgroundDrawable(new BitmapDrawable());