为什么线性布局的高度为零
我用 layout:height="fill_parent"
创建了 linearlayout
我设置了 3 个按钮和一个 TextView。我能够看到一切。但是当我想获得线性布局的高度时。我在 onCreate 方法中使用了以下代码。 我得到的线性布局高度为零。
但是我的线性布局高度是填充父级。我还可以看到我添加的元素。为什么我没有得到线性布局的实际高度。
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_linearlayout);
System.out.println("...Height..."+mainLayout.getMeasuredHeight());
谢谢 迪帕克
I have created linearlayout
with layout:height="fill_parent"
I have set 3 buttons and one TextView. I am able to see all. But when I want to get get the height of linearlayout. I used the following code in onCreate method. I am getting linear layout height as zero.
But My linearlayout height is fill parent. and also i am able to see elements that i have added. Why I am not getting the actual height of my linearlayout.
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_linearlayout);
System.out.println("...Height..."+mainLayout.getMeasuredHeight());
Thanks
Deepak
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
参考这个问题
Can't get height of Widgets
Refer to this question
Can't get height of Widgets
当一个 Activity 获得焦点时,它会被要求绘制它的布局。 Android 框架将处理绘制过程,但 Activity 必须提供其布局层次结构的根节点。
请参阅此链接查看绘图和布局内边距和边距
When an Activity receives focus, it will be requested to draw its layout. The Android framework will handle the procedure for drawing, but the Activity must provide the root node of its layout hierarchy.
See this link view drawing and layout padding and margis
直到onCreate、OnResume、onStart视图尚未创建。
因此,如果你想获取视图的高度或宽度,请执行以下操作并获取它
,请获取新的 TreeObserver 来删除,否则你会得到一个异常,TreeObserver 没有
覆盖 onWindowFocusChanged 方法,你会得到视图的高度或宽度。但是,如果您的活动作为子活动启动,则不会调用 onWindowFocusChanged 方法。所以在这种情况下请使用上面的那个。
Till onCreate, OnResume, onStart view is not yet created.
So if you want to get height or width of view do following and get it
Please get new TreeObserver to remove, otherwise you will get an exception, TreeObserver is not alive
overriding onWindowFocusChanged method, you get the height or width of view. But if your activity is started as child activity then onWindowFocusChanged method will not get called. So in that case use the above one.