Android: Get Screen size for the root Layout only

佐手、 提交于 2019-12-02 05:18:54

问题


Please get me correctly over here :

I want to get the height/width of the space available to the Activity/Layout in onCreate() method to calculate the height that can be given to child layouts. I can get the screen size using :

      root = (LinearLayout) findViewById(R.id.mainroot); // Main layout of LinearLayout  

       android.view.Display display = getWindowManager().getDefaultDisplay();
    int height = Display.getHeight(); // I know this is deprecated have hence used 
      int width = Display.getWidth(); // DisplayMetrics
    int childWidth, childHeight;

    DisplayMetrics metrics = new DisplayMetrics();
    display.getMetrics(metrics);
    //int density = metrics.densityDpi;
    height = metrics.heightPixels;  //480
    width = metrics.widthPixels;    //320

This both the methods/ways gives me same height and width i.e. size of full screen. What I am looking for is to get actual height that is avaialbe for the layout after deduction of Application Title, Status Bar, etc.

Any idea how to get this. OR to get the sizes of titles, etc - what all should be counted over here. On emulator I see 2 bars on top - 1 must be application titile what an be the other one. I can get heights of them all and deduct from screen height.

ONE more point : In this case I will be setting the height programamtically so it will be pixel based (as I can setheight in pixels only I guess) will that affect in density factor with differnet screen sizes. What can be a way to calculate height (lets say 50%) for child layout that will be same for any density o so.


回答1:


SOLUTION :

In my onCreate(), I added the following lines :

    setContentView(R.layout.mainpage);

    root = (LinearLayout) findViewById(R.id.mainroot);    
    root.post(new Runnable() {
        public void run() {
            Rect rect = new Rect();
            Window win = getWindow();
            win.getDecorView().getWindowVisibleDisplayFrame(rect);
            int statusHeight = rect.top;
            int contentViewTop = win.findViewById(Window.ID_ANDROID_CONTENT).getTop();
            titleHeight = contentViewTop - statusHeight;
            Log.i(Utility.TAG, "titleHeight = " + titleHeight + " statusHeight = " + statusHeight + " contentViewTop = " + contentViewTop);

            // CALCULATE THE SIZE OF INNER LAYOUTS
            calculateChildSize();
        }
    });

With the above code, I get the values of titleBar & statusBar. On deducting it from metrics.heightPixels; I get the required height of the screen.

Good Point is this code works for all density's.

Hope this helps others too.

FOR IMPROVEMENT : I have to do similar calculations for all Activities in my application, so was thinking about writing this code only once. I can save teh titleHeight to a static variable so can use in all activities. BUT

Can the user change the phone's density at runtime. If so, then the Activity's onCreate will be called again or not ? If not, then can I trap the density change event where I can add this code and make the current activity to refresh.

Any idea suggestions for improving is appreciated.




回答2:


//Where rootView is the object for the root view of your application final int viewWidt = rootView.getMeasuredWidth();



来源:https://stackoverflow.com/questions/8784616/android-get-screen-size-for-the-root-layout-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!