2013-07-09 42 views
3

我想在onCreate()方法中获取Middle(Body)布局的高度/宽度。Android:获取主体布局的大小动态

我的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是我的页脚布局。

我指的是this link。但我并不完全明白我在做什么?

+0

你有没有试过这个http://stackoverflow.com/questions/12068945/get-layout-height-and-width-at-run-time-android? – sandrstar

+0

我有设备的高度/宽度通过使用“显示”类。 – Lawrance

+0

根据你的问题你需要svtest布局的大小,对吧? – sandrstar

回答

1

首先,不要在onCreate()中获取视图尺寸,因为该视图可能尚未初始化。您可以使用onStart()方法执行此操作。所有的意见都会被夸大,并在该点布局在屏幕上:

@Override 
    protected void onStart() { 
     super.onStart(); 
     ScrollView scrollView = (ScrollView) findViewById(R.id.svtest); 
     int width = scrollView.getWidth(); 
     int height = scrollView.getHeight(); 
    } 
+0

我得到了width = 0和height = 0 .. – Lawrance

+0

你在onStart()中调用它吗? scrollview中是否有充气元素?如果它里面没有任何东西,它将返回0. –

+0

这是当宽度和高度设置为wrap_content和match_parent时那些不起作用的点 – hasan83

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(); 
} 
11

补充:这适用于所有类型的布局。不仅是ScrollView。当布局的宽度和高度被设置为match_parentwrap_content

getWidth()getHeight()方法返回0。尺寸不被测量。

用于获取测量尺寸的正确方法是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. 
    } 
}); 

一些答案试图做到这一点在onStart()方法。但是,他们试图拨打getWidth()getHeight()的方法。尝试getMeasuredWidth()getMeasuredHeight()。它的工作原理没有添加OnGlobalLayoutListener。只有在想要在create方法中获取尺寸时才需要侦听器。在任何后期阶段,听众都不需要,因为届时尺寸将被测量(例如开始)。

+0

该片段效果很好,但我怎么能在调用'addOnGlobalLayoutListener'的函数中获得'width'和'height',这样我就可以将值赋给这个函数中声明的'Point'? – statox

+0

int width = scrollView.getMeasuredWidth(); int height = scrollView.getMeasuredHeight();可以在onCreate方法之后的任何地方调用,以便您可以将它们添加到您的函数中。那是OnGlobalLayoutListener – hasan83

+0

的意思,你能接受我的回答吗?如果你认为它是正确的:) – hasan83