安卓:获取身体布局动态的大小 [英] Android: Get the size of the Body Layout Dynamically
问题描述
我想要得到的中间(身体)的高度/宽度的布局中的onCreate()方法。
我的main.xml是:
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / rl_Main
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=@可绘制/ main_bg11>
< RelativeLayout的
机器人:ID =@ + ID / rl_title
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentTop =真正的>
<包括布局=@布局/标题/>
< / RelativeLayout的>
<滚动型
机器人:ID =@ + ID / svtest
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_above =@ + ID / rl_music_controller
机器人:layout_below =@ + ID / rl_title>
< RelativeLayout的
机器人:ID =@ + ID / RL12
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:重力=中心>
< TableLayout
机器人:ID =@ + ID / tbl_vandana
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_centerHorizontal =真
机器人:layout_marginLeft =10dp
机器人:layout_marginRight =10dp
机器人:重力=center_horizontal
机器人:paddingTop =30dp>
< / TableLayout>
< / RelativeLayout的>
< /滚动型>
< RelativeLayout的
机器人:ID =@ + ID / rl_music_controller
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真正的>
<包括布局=@布局/控制器/>
< / RelativeLayout的>
我动态的TableLayout加入TextView中。 ID:
-
rl_title是我的标题布局
-
svtest是我的中间(身体)的内容。
-
rl_music_controller是我的页脚布局。
我指的<一个href="http://stackoverflow.com/questions/8784616/android-get-screen-size-for-the-root-layout-only">this链接。但我不明白到底我做什么?
补充:这适用于所有类型的布局。不仅滚动型。
的getWidth()
和的getHeight()
方法返回0时布局,宽度和高度设置为 match_parent
和 WRAP_CONTENT
。尺寸没有测定
正确的方法使用,以获得测量尺寸 getMeasuredWidth()
和 getMeasuredHeight()
。
注意:在onCreate方法测得的尺寸尚未初始化
。
正确的方法和地点(段可以加入任何地方,包括的onCreate
):
滚动型滚动视图=(滚动型)findViewById(R.id.svtest);
ViewTreeObserver VTO = scrollView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(新OnGlobalLayoutListener(){
@覆盖
公共无效onGlobalLayout(){
如果(Build.VERSION.SDK_INT&LT; 16)
。scrollView.getViewTreeObserver()removeGlobalOnLayoutListener(听众);
其他
。scrollView.getViewTreeObserver()removeOnGlobalLayoutListener(听众);
INT宽度= scrollView.getMeasuredWidth();
INT高= scrollView.getMeasuredHeight();
//推迟任何计算依赖于它在这里。
//不管它是什么。 UI或HTTP连接。
}
});
一些答案试图做到这一点的OnStart()方法。但是,他们试图拨打的getWidth()
和的getHeight()
的方法。尝试 getMeasuredWidth()
和 getMeasuredHeight()
。它应该工作!
I want to get the height/width of the Middle(Body) layout in onCreate() method.
My main.xml is :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/main_bg11" >
<RelativeLayout
android:id="@+id/rl_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<include layout="@layout/title" />
</RelativeLayout>
<ScrollView
android:id="@+id/svtest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/rl_music_controller"
android:layout_below="@+id/rl_title" >
<RelativeLayout
android:id="@+id/rl12"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center" >
<TableLayout
android:id="@+id/tbl_vandana"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center_horizontal"
android:paddingTop="30dp" >
</TableLayout>
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="@+id/rl_music_controller"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<include layout="@layout/controller" />
</RelativeLayout>
I am adding Textview dynamically in TableLayout. id :
rl_title is my title layout
svtest is my Middle(Body) Content.
rl_music_controller is my footer layout.
I am referring this link. But i don't understand exactly what i do?
Added: This applies for all types of layout. Not only ScrollView.
getWidth()
and getHeight()
methods returns 0 when layouts width and height are set as match_parent
and wrap_content
. dimensions are not measured.
The right methods to use to get measured dimensions are getMeasuredWidth()
and getMeasuredHeight()
.
Note: measured dimensions in onCreate method are not initialized yet.
The correct way and place is (snippet can be added anywhere including onCreate
):
ScrollView scrollView = (ScrollView)findViewById(R.id.svtest);
ViewTreeObserver vto = scrollView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < 16)
scrollView.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
else
scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
int width = scrollView.getMeasuredWidth();
int height = scrollView.getMeasuredHeight();
// postpone any calculation depend on it to here.
// regardless what it is. UI or http connection.
}
});
Some answers tried to do it onStart() method. but, they tried to call getWidth()
and getHeight()
methods. Try getMeasuredWidth()
and getMeasuredHeight()
. it should work!
这篇关于安卓:获取身体布局动态的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!