我们如何将Popup窗口设置为Top to 40 dip?

时间:2013-12-31 09:14:49

标签: android android-layout android-xml

如何将弹出窗口顶部设置为40 dip?

popup menu

我的弹出菜单代码

 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?

1 个答案:

答案 0 :(得分:1)

您正在将OFFSET_Y设置为40。该值以像素为单位。您需要在dp中设置值,因此您应该执行以下操作。将40dp转换为像素:

Resources r = getResources();
int OFFSET_Y = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 40, r.getDisplayMetrics());

休息保持不变。它应该工作正常。