customView与父高度和宽度不匹配

时间:2018-01-23 09:17:18

标签: android android-custom-view android-windowmanager

摘要

您好 我试图覆盖另一个视图上方的自定义膨胀视图, 对于大多数设备,它工作正常,自定义视图覆盖整个视图, 但出于某种原因,在屏幕尺寸为5.7的设备中,它并不存在 here is an example how it looks like for Nexus 6p

问题:
对于屏幕尺寸为5.7的设备,自定义视图与父高度和宽度不匹配。

  private void addCustomView(){

        LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        MyCustomView myCustomView = (MyCustomView) layoutInflater.inflate(R.layout.my_custom_view_layout, null);

        mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        mWindowManager.getDefaultDisplay().getMetrics(metrics);
        mWindowManager.addView(myCustomView, getLayoutParams());

    }

    private WindowManager.LayoutParams getLayoutParams(){
        if (mParentView == null) {
            return null;
        }
        int[] array = new int[2];
        mParentView.getLocationOnScreen(array);

        //Build Params
        int width = mParentView.getWidth();
        int height = mParentView.getHeight();

        WindowManager.LayoutParams mWindowParams = new WindowManager.LayoutParams();
        mWindowParams.gravity = Gravity.TOP | Gravity.LEFT;
        mWindowParams.x = array[0];
        mWindowParams.y = array[1];

        mWindowParams.height = height;
        mWindowParams.width = width;

        mWindowParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
                | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
        mWindowParams.format = PixelFormat.TRANSLUCENT;
        mWindowParams.windowAnimations = 0;

        return mWindowParams;
    }     

MyCustomView XML:

<?xml version="1.0" encoding="utf-8"?>
<com.app.myapp.MyCustomView xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"                                                   
                android:visibility="invisible">
    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:adjustViewBounds="true"/>

</com.app.myapp.MyCustomView>

0 个答案:

没有答案