Get screen width and height in Android

前端 未结 30 3681
野的像风
野的像风 2020-11-22 09:28

How can I get the screen width and height and use this value in:

@Override protected void onMeasure(int widthSpecId, int heightSpecId) {
    Log.e(TAG, \"onM         


        
30条回答
  •  醉酒成梦
    2020-11-22 10:24

    Just to update the answer by parag and SpK to align with current SDK backward compatibility from deprecated methods:

    int Measuredwidth = 0;  
    int Measuredheight = 0;  
    Point size = new Point();
    WindowManager w = getWindowManager();
    
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)    {
        w.getDefaultDisplay().getSize(size);
        Measuredwidth = size.x;
        Measuredheight = size.y; 
    }else{
        Display d = w.getDefaultDisplay(); 
        Measuredwidth = d.getWidth(); 
        Measuredheight = d.getHeight(); 
    }
    

提交回复
热议问题