获取屏幕的高和宽
DisplayMetrics dm = getResources().getDisplayMetrics();int width = dm.widthPixels;int height = dm.heightPixels;
?上面的方法是获取整个屏幕的高度和宽度,可是有的时候Activity被嵌套使用,所以需要获得Activity的高度,而不是整个屏幕的高宽
?
?
获取某一控件的高度和宽度:
可以将Activity所对应的layout的最外层的LinearLayout设置一个id,找到这个Id就可以获得Activity的高度
?
int height = this.getWindow().findViewById(R.id.activity1_ll).getHeight();int width = this.getWindow().findViewById(R.id.activity1_ll).getWidth();
?
每个View都有一个getLocationOnScreen()方法,这个方法可以获得该View在屏幕上面的绝对位置,从View对象的左上角计算
?
LinearLayout ll = (LinearLayout)this.getWindow().findViewById(R.id.activity1_ll);int[] location = new int[2];ll.getLocationOnScreen(location);System.out.println("location[0]"+location[0]);//代表宽度System.out.println("location[1]"+location[1]);//代表高度
?
这样也可以计算出Title所占据的高度了
?
?
?
/** * 得到地图的高度和宽度 * */public void getMapViewHeightAndWidth(){mapView.post(new Runnable(){//获得地图的宽高public void run() {heightOfMapView = mapView.getHeight();widthOfMapView = mapView.getWidth();System.out.println("heightOfMapView:" + heightOfMapView + "widthOfMapView:" + widthOfMapView);}});}?
?