Popupwindow全屏问题

50 篇文章 0 订阅

popup_window.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:background="#ddffffff"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">

    <Button
            android:text="haha"
            android:id="@+id/bt_haha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    <Button
            android:text="bottom"
            android:layout_alignParentBottom="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

</RelativeLayout>


public void showMenu() {

        Log.e("TAG","showMenu");
        View popView = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.popup_windiow, mRootView, false);
        /*
        Rect rect = new Rect();
        Point offset = new Point();
        mRootView.getGlobalVisibleRect(rect,offset);
        rect.offset(-offset.x,-offset.y);
        */
        final PopupWindow pop = new PopupWindow(popView, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT,true);
        pop.setBackgroundDrawable(new BitmapDrawable());

        popView.findViewById(R.id.bt_haha).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });

        pop.showAtLocation(mRootView, Gravity.CENTER,0,getStatusBarHeight());

    }
public int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }


现在出现的问题是,popwindow会超出屏幕边距


解决方案是,计算屏幕大小与statusbar的距离
public class Screen {

    private int width;
    private int height;
    public Screen(Activity context) {
        DisplayMetrics metrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metrics );
        width = metrics.widthPixels;
        height = metrics.heightPixels;
    }
    /**
     * @return 屏幕宽度 in pixel
     */
    public int getWidth() {
        return width;
    }

    /**
     *
     * @return 屏幕高度 in pixel
     */
    public int getHeight() {
        return height;
    }

    @Override
    public String toString() {
        return "Screen{" +
                "width=" + width +
                ", height=" + height +
                '}';
    }
}


public void showMenu() {

        Log.e("TAG","showMenu");
        View popView = ((LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE)).inflate(R.layout.popup_windiow, mRootView, false);
        /*
        Rect rect = new Rect();
        Point offset = new Point();
        mRootView.getGlobalVisibleRect(rect,offset);
        rect.offset(-offset.x,-offset.y);
        */
        final PopupWindow pop = new PopupWindow(popView, mScreen.getWidth(), mScreen.getHeight()-getStatusBarHeight(),true);
        pop.setBackgroundDrawable(new BitmapDrawable());

        popView.findViewById(R.id.bt_haha).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop.dismiss();
            }
        });

        pop.showAtLocation(mRootView, Gravity.NO_GRAVITY,0,getStatusBarHeight());

    }


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值