简体   繁体   English

Android:仅获取根布局的屏幕大小

[英]Android: Get Screen size for the root Layout only

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.我想在 onCreate() 方法中获取活动/布局可用空间的高度/宽度,以计算可以给子布局的高度。 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 ie 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.在模拟器上,我看到顶部有 2 个条 - 1 个必须是应用程序标题,另一个是什么。 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.有什么方法可以计算对于任何密度都相同的子布局的高度(比如说 50%)。

SOLUTION:解决方案:

In my onCreate(), I added the following lines:在我的 onCreate() 中,我添加了以下几行:

    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.通过上面的代码,我得到了 titleBar & statusBar 的值。 On deducting it from metrics.heightPixels;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.我可以将 titleHeight 保存到 static 变量中,以便在所有活动中使用。 BUT

Can the user change the phone's density at runtime.用户能否在运行时更改手机的密度。 If so, then the Activity's onCreate will be called again or not?如果是这样,那么Activity的onCreate是否会被再次调用? 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.任何改进的想法建议表示赞赏。

There is very easy method for that.有一个非常简单的方法。 doOnLayout() method is called as soon as layout is measured and ready:测量并准备好布局后立即调用 doOnLayout() 方法:

rootView.doOnLayout {
    rootView.getMeasuredWidth()
    rootView.getMeasuredHeight()
}

//Where rootView is the object for the root view of your application final int viewWidt = rootView.getMeasuredWidth(); //其中 rootView 是 object 用于您的应用程序的根视图 final int viewWidt = rootView.getMeasuredWidth();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM