使用BitmapDrawable的自定义PopupWindow方法已经被弃用

4
在我的应用程序中使用自定义PopupWindow,我正在使用
popup.setBackgroundDrawable(new BitmapDrawable());

方法。现在这个方法已经被弃用了,没有这个方法,我就不能为我的弹出窗口提供背景。我从一篇文章中读到它的替代方法是

popup.setsetBackgroundDrawable(new BitmapDrawable(Context.Resources,Drawable);

但是在这里我没有使用任何可绘制的东西来制作我的弹出窗口。我的代码如下,我正在制作自定义Popupwindow,在这里解决这个问题。

@Override
    public void onWindowFocusChanged(boolean hasFocus) {

        int[] location = new int[2];
        Button button = (Button) findViewById(R.id.VBG_img_pin_x);

        // Get the x, y location and store it in the location[] array
        // location[0] = x, location[1] = y.
        button.getLocationOnScreen(location);

        // Initialize the Point with x, and y positions
        p = new Point();
        p.x = location[0];
        p.y = location[1];
    }

    // The method that displays the popup.
    private void showPopup(final Activity context, Point p) {

//      Display display = getWindowManager().getDefaultDisplay();

        DisplayMetrics display = this.getResources().getDisplayMetrics();

        int width = display.widthPixels;
        int height = display.heightPixels;

//      int width = display.getWidth(); // deprecated
//      int height = display.getHeight(); // deprecated

        int popupWidth = width / 2;
        int popupHeight = height;

        ChatUtils.getScreenHeight(getBaseContext());

        // Inflate the popup_layout.xml
        LinearLayout viewGroup = (LinearLayout) context
                .findViewById(R.id.popup);

        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

        onSetAlpha(240, layout);
//      layout.setAlpha((float) 0.95);

        // Creating the PopupWindow
        final PopupWindow popup = new PopupWindow(context);

        popup.setTouchable(true);
        popup.setFocusable(false);
        popup.setOutsideTouchable(true);
        popup.setContentView(layout);
        popup.setWidth(popupWidth);
        popup.setHeight(popupHeight);
        popup.setFocusable(true);

        // Some offset to align the popup a bit to the right, and a bit down,
        // relative to button's position.
        // Clear the default translucent background
        popup.setBackgroundDrawable(new BitmapDrawable());

}

1
popup.setBackgroundDrawable(null);应具有相同的效果。你可以试试吗? - Blackbelt
@Blackbelt,感谢您的评论。它有效。请在我的答案中回答这个问题,这样我就可以接受它作为我的答案。 - Muhammad Usman Khan
2个回答

6
如果想要清除背景,如您的评论所述,
//清除默认半透明背景
您可以使用
popup.setBackgroundDrawable(null);

我将不使用空值选项。这会在某些Android版本(例如5.1.1)中带来触摸问题,其中TouchInterceptor根本没有被调用。 - RedDeath

2

如果您想要着色背景,请使用此选项

 popup.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.white)));

这不是已弃用的。


getColor()在最新版本中已被弃用。 - Farya

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接