关于动态获取一个布局高度的疑问

dali_yan 2014-09-24 05:54:27
先看我的XML布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
android:background="#907B7E7B"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:id="@+id/control"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#077AA7"
android:gravity="center"
android:padding="20dp">
<ImageView
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:contentDescription="播放"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="@drawable/player_pause"/>

.............

</LinearLayout>
</LinearLayout>


在onCreate()中获取其中某一个布局的高度,例如获取id="@+id/content"的高度,使用的代码:
LinearLayout content = (LinearLayout) findViewById(R.id.content);
int height=content.getHeight()

可是在onCreate()获取到的高度却height=0,我知道这应该跟 android:layout_weight="1"这个属性的动态计算有关,现在我的问题是如果需要获取布局完后的高度,我需要在哪个生命周期中获取,也就是在哪个生命周期后屏幕的动态布局结束?
...全文
85 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
红袖满楼招 2014-09-24
  • 打赏
  • 举报
回复
View组件布局要在onResume回调后才会加载。也可以用getViewTreeObserver().addOnGlobalLayoutListener在oncreate中获取

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧