DisplayMetrics metrics = new DisplayMetrics();
Rect outRect = new Rect();
Timer timer = new Timer();
Display display;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mainLayout = (AbsoluteLayout)this.findViewById(R.id.main_layout);
this.getWindowManager().getDefaultDisplay().getMetrics(metrics);
Log.i("smart_home", "metrics::density=" + metrics.density
+", densityDpi="+metrics.densityDpi
+", heightPixels="+metrics.heightPixels
+", widthPixels="+metrics.widthPixels
+", scaledDensity="+metrics.scaledDensity
+", xdpi="+metrics.xdpi
+", ydpi="+metrics.ydpi);
timer.schedule(new PrintTask(), 100, 100);
}
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg)
{
super.handleMessage(msg);
addTitle();
addNavigation();
addContent();
}
};
class PrintTask extends TimerTask
{
@Override
public void run()
{
mainLayout.getWindowVisibleDisplayFrame(outRect);
Log.i("smart_home", "outRect::top=" + outRect.top
+ ", bottom=" + outRect.bottom
+ ", left=" + outRect.left
+ ", right=" + outRect.right
+ ", width=" + outRect.width()
+ ", height=" + outRect.height());
activityWidth = outRect.width();
activityHeight = outRect.height();
display = getWindowManager().getDefaultDisplay();
Log.i("smart_home", "display::width="+display.getWidth()+", height="+display.getHeight());
if(activityHeight != 0)
{
handler.sendEmptyMessage(0);
timer.cancel();
}
}
}
结果--SONY手机
10-17 22:43:29.194: INFO/smart_home(5028): metrics::density=1.5, densityDpi=240, heightPixels=569, widthPixels=320, scaledDensity=1.5, xdpi=239.05882, ydpi=243.72585
10-17 22:43:29.344: INFO/smart_home(5028): outRect::top=25, bottom=854, left=0, right=480, width=480, height=829
10-17 22:43:29.344: INFO/smart_home(5028): display::width=320, height=569
结果--LG手机
10-17 23:04:52.965: INFO/smart_home(3829): metrics::density=1.0, densityDpi=160, heightPixels=480, widthPixels=320, scaledDensity=1.0, xdpi=159.37254, ydpi=160.42105
10-17 23:04:53.075: INFO/smart_home(3829): outRect::top=25, bottom=480, left=0, right=320, width=320, height=455
10-17 23:04:53.075: INFO/smart_home(3829): display::width=320, height=480
在AndroidManifest.xml文件中添加元素support-screens
运行程序打印出log日志
10-20 22:23:10.875: INFO/smart_home(9560): metrics::density=1.5, densityDpi=240, heightPixels=854, widthPixels=480, scaledDensity=1.5, xdpi=239.05882, ydpi=243.72585
10-20 22:23:10.985: INFO/smart_home(9560): outRect::top=38, bottom=854, left=0, right=480, width=480, height=816
10-20 22:23:10.985: INFO/smart_home(9560): display::width=480, height=854