Android如何获取应用程序区域的大小

时间:2015-02-15 04:54:26

标签: android

Nexus6

对于上面的模拟器,在我的onCreate方法中,如何获得空白区域的大小(高度和宽度)? (不包括Project1 Screen2'空格)?

1 个答案:

答案 0 :(得分:2)

初始化视图并在创建方法

中调用此方法
 view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @SuppressLint("NewApi")
     @SuppressWarnings("deprecation") 
    @Override 
    public void onGlobalLayout() {
     //now we can retrieve the width and height 
    int width = view.getWidth(); 
    int height = view.getHeight(); 
    //... //do whatever you want with them //... //this is an important step not to keep receiving callbacks: //we should remove this listener //I use the function to remove it based on the api level!
     if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
     view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
     else
     view.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
    } });