Android:动态获取主体布局的大小

Android:动态获取主体布局的大小,android,layout,Android,Layout,我想在onCreate()方法中获得中间(主体)布局的高度/宽度 我的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="@drawab

我想在onCreate()方法中获得中间(主体)布局的高度/宽度

我的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。 身份证:

  • rl_title是我的标题布局

  • svtest是我的中间(身体)内容

  • RLU音乐控制器是我的页脚布局


  • 我指的是。但是我不太明白我到底在做什么?

    首先,不要在
    onCreate()
    中获取视图维度,因为视图可能尚未初始化。您可以在
    onStart()
    方法中执行此操作。此时,所有视图都已充气并在屏幕上显示:

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

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

    public void onWindowFocusChanged (boolean hasFocus)
    {
         super.onWindowFocusChanged(hasFocus);
         ScrollView scrollView = (ScrollView) findViewById(R.id.svtest);
         int width = scrollView.getWidth();
         int height = scrollView.getHeight();
    }
    
    添加:这适用于所有类型的布局。不仅仅是滚动视图

    getWidth()
    getHeight()
    当布局宽度和高度设置为
    match\u parent
    wrap\u content
    时,方法返回0。尺寸未测量

    获取测量尺寸的正确方法是
    getMeasuredWidth()
    getMeasuredHeight()

    注意:onCreate方法中的测量尺寸尚未初始化

    正确的方法和位置是(代码段可以添加到任何地方,包括
    onCreate
    ):

    ScrollView ScrollView=(ScrollView)findViewById(R.id.svtest);
    ViewTreeObserver vto=scrollView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(新的OnGlobalLayoutListener(){
    @凌驾
    public void onGlobalLayout(){
    如果(Build.VERSION.SDK_INT<16)
    scrollView.getViewTreeObserver().RemoveGlobalOnlyOutliner(侦听器);
    其他的
    scrollView.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
    
    int width=scrollView.getMeasuredWidth(); int height=scrollView.getMeasuredHeight(); //推迟任何计算取决于它在这里。 //不管它是什么。UI或http连接。 } });

    一些答案尝试在start()方法上执行此操作。但是,他们试图调用
    getWidth()
    getHeight()
    方法。尝试
    getMeasuredWidth()
    getMeasuredHeight()
    。它不需要添加
    OnGlobalLayoutListener
    。仅当您希望在on create方法中获取维度时,才需要侦听器。在以后的任何阶段,都不需要监听器,因为届时将测量尺寸(例如在开始时)。

    您尝试过吗?我使用“显示”类获得设备高度/宽度。根据您的问题,您需要svtest布局的大小,对吗?是的。。我计算正文部分的概念是:计算标题和页脚布局高度。我有整个装置的高度。从整个设备高度中扣除。这是正确的方法吗?是的,这是可能的方法,但使用提供的链接,您可能可以直接获得svtest布局的大小。是否在onStart()中调用它?scrollview中是否有膨胀的元素?如果里面没有任何内容,它将返回0。这是当宽度和高度设置为“包裹内容”和“匹配父项”时不起作用的点。这不起作用。在onStart中,无论什么维度,我都只得到0。代码段工作得很好,但是如何才能在调用
    addOnGlobalLayoutListener
    的函数中得到
    width
    height
    ,这样我就可以将值分配给该函数中声明的
    点了?int width=scrollView.getMeasuredWidth();int height=scrollView.getMeasuredHeight();可以在onCreate方法之后的任何位置调用,以便将它们添加到函数中。这就是OnGlobalYoutListeneri从
    onStart
    调用
    getMeasuredWidth
    的要点,我仍然得到
    0
    。OnGlobalLayoutListener的
    是必需的。
    
    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.
        } 
    });