方法一、
WindowManager manage=getWindowManager();
Display display=manage.getDefaultDisplay();
int screenHeight=display.getHeight();
int screenWidth=display.getWidth();
方法二、
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
dm.widthPixels,dm.heightPixels
本质上两个方法是差不多的。
****************************************************************************
Android如何获取屏幕分辨率的例子程序代码如下:
package com.acquire.resolution;
import Android.app.Activity;
import Android.content.res.Resources;
import Android.graphics.Color;
import Android.graphics.drawable.Drawable;
import Android.os.Bundle;
import Android.util.DisplayMetrics;
import Android.widget.TextView;
public class AcquireResolution extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.textView);
Resources resources = getBaseContext().getResources();
Drawable drawable = resources.getDrawable(R.drawable.red);
tv.setBackgroundDrawable(drawable);
tv.setTextColor(Color.GREEN);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
tv.setText("屏幕分辨率为:"+dm.widthPixels+" * "+dm.heightPixels);
}
}
package com.acquire.resolution;
import Android.app.Activity;
import Android.content.res.Resources;
import Android.graphics.Color;
import Android.graphics.drawable.Drawable;
import Android.os.Bundle;
import Android.util.DisplayMetrics;
import Android.widget.TextView;
public class AcquireResolution extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView)findViewById(R.id.textView);
Resources resources = getBaseContext().getResources();
Drawable drawable = resources.getDrawable(R.drawable.red);
tv.setBackgroundDrawable(drawable);
tv.setTextColor(Color.GREEN);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
tv.setText("屏幕分辨率为:"+dm.widthPixels+" * "+dm.heightPixels);
}
}
运行结果如下: