2 myq 26 myq_26 于 2012.11.30 17:03 提问

Android中获取屏幕宽度的方法

使用以下代码中的值如何获取屏幕的宽和高:

@Override protected void onMeasure(int widthSpecId, int heightSpecId) {
   Log.e(TAG, "onMeasure" + widthSpecId);
   setMeasuredDimension(SCREEN_WIDTH, SCREEN_HEIGHT - 
      game.findViewById(R.id.flag).getHeight());
}

2个回答

CSDN_Ask
CSDN_Ask   2012.11.30 17:48
已采纳

使用这段代码中,您可以运行时显示屏幕的宽度和高度:

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int wwidth = displaymetrics.widthPixels;
Baby_Bonnie
Baby_Bonnie   2012.12.03 13:20

只需要通过parag和SpK更新答案:

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(); 
    }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!