3

我想在 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>

我在 TableLayout 中动态添加 Textview。ID :

  1. rl_title 是我的标题布局

  2. svtest 是我的中间(正文)内容。

  3. rl_music_controller 是我的页脚布局。

我指的是这个链接。但我不明白我到底在做什么?

4

3 回答 3

11

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 works without adding OnGlobalLayoutListener. The listener is only required if you want to get the dimensions in the on create method. in any later stage the listener is not required because by then the dimensions will be measured(on start for example).

于 2013-10-24T09:18:40.490 回答
1

首先,不要获取视图尺寸,onCreate()因为视图可能尚未初始化。你可以在onStart()方法中做到这一点。此时所有视图都将被放大并显示在屏幕上:

@Override
    protected void onStart() {
        super.onStart();
        ScrollView scrollView = (ScrollView) findViewById(R.id.svtest);
        int width = scrollView.getWidth();
        int height = scrollView.getHeight();
    }
于 2013-07-09T07:09:35.277 回答
0

我发现重写以下方法可以防止宽度和高度为0。在活动的这个阶段,活动中的所有视图都应该被膨胀。

public void onWindowFocusChanged (boolean hasFocus)
{
     super.onWindowFocusChanged(hasFocus);
     ScrollView scrollView = (ScrollView) findViewById(R.id.svtest);
     int width = scrollView.getWidth();
     int height = scrollView.getHeight();
}
于 2013-07-09T07:26:11.963 回答