Android:仅获取根布局的屏幕尺寸

发布于 2024-12-25 09:47:43 字数 962 浏览 4 评论 0原文

请正确理解我的意思:

我想在 onCreate() 方法中获取活动/布局可用的空间的高度/宽度,以计算可以赋予子布局的高度。我可以使用以下方法获取屏幕尺寸:

      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

这两种方法/方式都给了我相同的高度和宽度,即全屏尺寸。我正在寻找的是在扣除应用程序标题、状态栏等之后获得布局可用的实际高度

。知道如何获得它。或者获取标题的大小等 - 所有这些都应该在这里计算。在模拟器上,我看到顶部有 2 个栏 - 1 个必须是应用程序标题,另一个是应用程序标题。我可以得到它们的高度并从屏幕高度中扣除。

还有一点:在这种情况下,我将以编程方式设置高度,因此它将基于像素(因为我猜只能以像素为单位设置高度),这会影响不同屏幕尺寸的密度因子。有什么方法可以计算子布局的高度(比方说 50%),该高度对于任何密度都相同。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不离久伴 2025-01-01 09:47:43

解决方案:

在我的 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();
        }
    });

通过上面的代码,我得到了 titleBar 和 titleBar 的值。状态栏。从 metrics.heightPixels; 中扣除它,我得到了所需的屏幕高度。

优点是该代码适用于所有密度。

希望这也对其他人有帮助。

改进:我必须对应用程序中的所有活动进行类似的计算,因此考虑只编写一次此代码。我可以将 titleHeight 保存到静态变量中,以便可以在所有活动中使用。
但是

用户可以在运行时更改手机的密度吗?
如果是这样,那么Activity的onCreate是否会再次被调用?
如果没有,那么我可以捕获密度更改事件,在其中添加此代码并使当前活动刷新。

任何改进的想法建议都将受到赞赏。

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.

Spring初心 2025-01-01 09:47:43

//其中 rootView 是应用程序根视图的对象
最终 int viewWidt = rootView.getMeasuredWidth();

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

暖心男生 2025-01-01 09:47:43

有一个非常简单的方法。一旦布局被测量并准备好, doOnLayout() 方法就会被调用:

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

There is very easy method for that. doOnLayout() method is called as soon as layout is measured and ready:

rootView.doOnLayout {
    rootView.getMeasuredWidth()
    rootView.getMeasuredHeight()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文