android 在 onCreate 方法中获取可绘制区域

发布于 2024-12-03 01:00:13 字数 120 浏览 6 评论 0原文

如果我有一个高度和宽度为 fill_parent 的布局,有没有办法在 onCreate 方法中获取他的高度和宽度?如果没有,他的尺寸什么时候可用?我需要这个,因为我需要在运行时添加一些视图,并且我需要知道有多少视图填充布局。

if I have a layout with fill_parent at height and width, is there a way to get his height and width in onCreate method? if not, when his dimensions are available? I need this because I need to add at runtime some views and I need to know how many views fill in the layout.

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

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

发布评论

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

评论(2

江南烟雨〆相思醉 2024-12-10 01:00:13

你可以这样做:。
假设您在 onCreate 方法中有布局视图

     layout.post(new Runnable() {
        @Override
          public void run() {
            height= getHeight();
            width=getWidth();        
            //pupulate other views now that you know the parent size     
            }
        }
  });

You can do that:.
Suppose you have the layout view in onCreate method

     layout.post(new Runnable() {
        @Override
          public void run() {
            height= getHeight();
            width=getWidth();        
            //pupulate other views now that you know the parent size     
            }
        }
  });
小忆控 2024-12-10 01:00:13

您可以使用 onSizeChanged 方法在计算后获取信息,但这也可能有助于至少获取显示的尺寸。

Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
int width = display.getWidth(); 

您可能会发现这很有用:

何时调用 onSizeChanged()?

You can use the onSizeChanged method to get the information when it has been calculated, but this may also help to at least get the dimensions of the display.

Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
int width = display.getWidth(); 

You may find this useful:

when is onSizeChanged() called?

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