安卓Activity的高度和宽度

 

 

 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值