Android弹出菜单阴影

Android弹出菜单阴影,android,menu,popup,shadow,Android,Menu,Popup,Shadow,我创建了这个弹出菜单,但是背景阴影丢失了。我怎样才能添加一些?如果阴影只在左侧和底部,那就太酷了 下面是一张图片:您可以看到弹出窗口的颜色和工具栏下方活动的背景齐头并进 这是我的密码: 活动片段 public void showPopup(final MenuItem menuItem) { View view = findViewById(R.id.action_alarm); LayoutInflater layoutInflater = (LayoutI

我创建了这个弹出菜单,但是背景阴影丢失了。我怎样才能添加一些?如果阴影只在左侧和底部,那就太酷了

下面是一张图片:您可以看到弹出窗口的颜色和工具栏下方活动的背景齐头并进

这是我的密码:

活动片段

public void showPopup(final MenuItem menuItem) {
        View view = findViewById(R.id.action_alarm);

        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popupView = layoutInflater.inflate(R.layout.popup, null);
        final ListView listView = (ListView) popupView.findViewById(R.id.listView);
        String[] functions = {getString(R.string.benachrichtigung), getString(R.string.benachrichtigungUm)};
        final ListAdapter adapter = new CustomPopupAdapter(this, functions, listView);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView tv = (TextView) listView.getChildAt(1).findViewById(R.id.tvTime);
                showTimePickerDialog(tv);
            }
        });

        PopupWindow popupWindow = new PopupWindow(
                popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.showAsDropDown(view);
    }

几天前我遇到了同样的问题:)我就是这样解决的。下面的链接将带您进入一个网站,在那里您可以生成任何您想要的阴影:)

这是一个9补丁映像:)一旦完成,您需要做的就是:)


就这样:)完成了:)

我认为最简单的方法是设置视图的高程,该高程在API 21及更高版本中可用。这对我来说很有效:

if (Build.VERSION.SDK_INT >= 21) {
    popupWindow.setElevation(10);
}

您可以将RelativeLayout的背景更改为带有阴影的照片或形状。@tinysunlight您可以发布一个示例吗?您可以使用popupwindow而不是popupmenu,popupwindow比PopupMenuu有更多功能使用此布局,因为父LayoutgetDrawable()需要api 21。我的最小值是16。如果您仍然有问题,请尝试此ContextCompat.getDrawable(context,R.drawable.**)以下是您也可以查看的链接:)Ok ContextCompat有效。但是阴影没有显示。确定:)一个小改动:)你不能使用popupWindow.ShowAs下拉菜单(视图);而是使用popupWindow.showAtLocation()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.shadow_192256));
        } else {
            popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.shadow_192256));
        }
  Display display = (yourActivity.getWindowManager().getDefaultDisplay();
  Point size = new Point();
  display.getSize(size);
  int width = size.x;
  int height = size.y;

  Resources resources = yourActivity.getResources();
  int navigationBarHeight = 0;
  int statusbarHeight = 0;
  int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
  if (resourceId > 0) {
      navigationBarHeight = resources.getDimensionPixelSize(resourceId);
   }

  resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
  if (resourceId > 0) {
       statusbarHeight = resources.getDimensionPixelSize(resourceId);
            }
 popupWindow = new PopupWindow(yourActivity);
 popupWindow.setContentView(yourlayout);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                popupWindow.setBackgroundDrawable(resources.getDrawable(R.drawable.shadow, yourActivity.getTheme()));
            } else {
                popupWindow.setBackgroundDrawable(resources.getDrawable(R.drawable.shadow));
            }
            popupWindow.setWidth(width - 20);//20 is padding i have added 10 from right 10 from left
            popupWindow.setHeight(height - (navigationBarHeight +40));
            popupWindow.setOutsideTouchable(true);
            popupWindow.setFocusable(true);   
            popupWindow.showAtLocation(youractivityView, Gravity.CENTER, 0, statusbarHeight);
if (Build.VERSION.SDK_INT >= 21) {
    popupWindow.setElevation(10);
}