我正在使用 PopupWindow 在单击 ListView 项目时显示一些子类别。
我对 ListView
行(类别)和 PopupWindow(类别)使用相同的布局。
我的问题是我将 PopupWindow
的宽度设置为我的行项目 View 的宽度,但是当它显示时,它有点窄,带有某种边框。
在下面的屏幕截图中,我将 PopupWindow 放在 ListView 的顶部以查看区别
为什么变小了?我怎样才能去除这个边框?
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);
}
最佳答案
我必须使用以下方法删除 PopupWindow 的背景:
mPopup.setBackgroundDrawable(new BitmapDrawable());
关于android - PopupWindow 的宽度/样式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9826132/