如何将弹出窗口顶部设置为40 dip?
我的弹出菜单代码
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.llll);
LayoutInflater layoutInflater =
(LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.action_bar, null);
// Creating the PopupWindow
changeStatusPopUp = new PopupWindow(context);
changeStatusPopUp.setContentView(layout);
changeStatusPopUp.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
changeStatusPopUp.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
changeStatusPopUp.setFocusable(true);
int OFFSET_X = -20;
int OFFSET_Y = 40;
changeStatusPopUp.showAtLocation(layout,
Gravity.TOP|Gravity.LEFT, Gravity.LEFT,OFFSET_Y);
请建议我如何将弹出式菜单设置为40 dip?
答案 0 :(得分:1)
您正在将OFFSET_Y
设置为40
。该值以像素为单位。您需要在dp
中设置值,因此您应该执行以下操作。将40dp
转换为像素:
Resources r = getResources();
int OFFSET_Y = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, r.getDisplayMetrics());
休息保持不变。它应该工作正常。