How can I get android Honeycomb system's screen width and height?

前端 未结 3 1552
别跟我提以往
别跟我提以往 2020-12-17 05:41

my code:

Display display = activity.getWindowManager().getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getMetrics(displayM         


        
3条回答
  •  北荒
    北荒 (楼主)
    2020-12-17 06:17

    This piece of code works for me (if you don't mind to use undocumented methods):

    Display d = getWindowManager().getDefaultDisplay();
    int width, height;
    
    Method mGetRawH;
    Method mGetRawW;
    try {
        mGetRawH = Display.class.getMethod("getRawWidth");
        mGetRawW = Display.class.getMethod("getRawHeight");
        width = (Integer) mGetRawW.invoke(d);
        height = (Integer) mGetRawH.invoke(d);
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        e.printStackTrace();
    }
    
    //You should care about orientation here
    int o = getResources().getConfiguration().orientation;
    if (o == Configuration.ORIENTATION_PORTRAIT) {
        if (width > height) {
            int tmp = width;
            width = height;
            height = tmp;
        }
    } else if (o == Configuration.ORIENTATION_LANDSCAPE) {
        if (width < height) {
            int tmp = width;
            width = height;
            height = tmp;
        }
    }
    
    Log.d("d", "w=" + width + " h=" + height);
    

提交回复
热议问题