Android App on Chrome OS does not get the proper DisplayMetrics window height

社会主义新天地 提交于 2020-12-15 03:41:34

问题


I need to get the height of the available Android app's screen on a Chrome OS device. I am using DisplayMetrics to get the height dimension. However, displayMetrics.heightPixels does not get the full height of the app screen on Chrome OS devices. There is always a small gap on Chrome OS devices.

This can be visualized using the WindowManager example I have provided below. Just copy and paste this into your main activity and run the app in a Chrome OS device (I used Google Pixel Slate) and compare it to the app running on Android. To visualize this with WindowManger set boolean shouldVisualize = true; To turn off the visualization set boolean shouldVisualize = false;

The displayMetrics.heightPixels is 100% correct on all Android devices. But there is always a little bit subtracted on Chrome OS devices. How do I get the correct screen height on Chrome OS?

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        boolean shouldVisualize = true;

        if (shouldVisualize) {
            LinearLayout overlayVisual = new LinearLayout(this);
            int visualColor = Color.parseColor("#aa4444");
            overlayVisual.setBackgroundColor(visualColor);

            WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
            DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

            int screenWidth = displayMetrics.widthPixels;
            int screenHeight = displayMetrics.heightPixels;

            WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                    screenWidth,
                    screenHeight,
                    WindowManager.LayoutParams.TYPE_APPLICATION,
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                    PixelFormat.TRANSLUCENT);
            params.gravity = Gravity.LEFT | Gravity.TOP;
            params.x = 0;
            params.y = 0;
            
            windowManager.addView(overlayVisual, params);
        }
    }
}

Note that the goal isn't to make the overlay fit the screen but rather to get the correct screen dimensions with DisplayMetrics (so using WindowManager.LayoutParams.MATCH_PARENT is not the answer). This problem only occurs on Chrome OS tablets. The gap seems to be similar to the height of the status bar at the bottom of Chrome OS. Perhaps DisplayMetrics is improperly accounting for the Chrome OS status bar. But if this is the case, it is subtracting the status bar twice instead of once.

Screenshot for boolean shouldVisualize = false; This is just a preview before I visualize the problem in the next screenshot.

Screenshot for boolean shouldVisualize = true; Please notice the gap at the bottom.

来源:https://stackoverflow.com/questions/65164189/android-app-on-chrome-os-does-not-get-the-proper-displaymetrics-window-height

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!