我想在onCreate()方法中获得Middle(Body)布局的高度/宽度.
我的main.xml是:
<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>
Run Code Online (Sandbox Code Playgroud)
我在TableLayout中动态添加Textview.ID :
rl_title是我的标题布局
svtest是我的中间(身体)内容.
rl_music_controller是我的页脚布局.
我指的是这个链接.但我不明白我到底做了什么?
has*_*san 11
补充:这适用于所有类型的布局.不仅是ScrollView.
getWidth()getHeight()当布局宽度和高度设置为match_parent和时,方法返回0 wrap_content.尺寸未测量.
用于获得测量尺寸的正确方法是getMeasuredWidth()和getMeasuredHeight().
注意: onCreate方法中的测量尺寸尚未初始化.
正确的方式和地点是(片段可以添加到任何地方,包括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.
}
});
Run Code Online (Sandbox Code Playgroud)
一些答案试图在onStart()方法上做到这一点.但是,他们试图打电话getWidth()和getHeight()方法.尝试getMeasuredWidth()和getMeasuredHeight().它没有添加OnGlobalLayoutListener.只有在想要在on create方法中获取维度时才需要监听器.在任何后期阶段,不需要监听器,因为那时将测量尺寸(例如,在开始时).
| 归档时间: |
|
| 查看次数: |
10197 次 |
| 最近记录: |