Android getMeasuredHeight()和在measure()之后返回0的宽度

Android getMeasuredHeight()和在measure()之后返回0的宽度,android,android-layout,Android,Android Layout,我真的很困惑android对布局的测量。基本上,我想要得到一个视图的实际计算高度和宽度,当它在布局之前已经被布局好了。我需要获得计算出的高度和宽度,因为我有一个隐藏的LinearLayout,我想在使用viewpropertyanimator打开时设置动画。我需要在动画制作之前将目标宽度(即计算的宽度)提供给动画师。这个布局的宽度是动态的,因为它依赖于宽度的android:weight参数,所以我不能给出静态值 这是我的活动课: package com.example.testproject;

我真的很困惑android对布局的测量。基本上,我想要得到一个视图的实际计算高度和宽度,当它在布局之前已经被布局好了。我需要获得计算出的高度和宽度,因为我有一个隐藏的
LinearLayout
,我想在使用viewpropertyanimator打开时设置动画。我需要在动画制作之前将目标宽度(即计算的宽度)提供给动画师。这个布局的宽度是动态的,因为它依赖于宽度的
android:weight
参数,所以我不能给出静态值

这是我的活动课:

package com.example.testproject;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class TestActivity extends Activity
    {

    private LinearLayout gmailListContainer, yahooMailListContainer, windowsLiveListContainer;
    private LinearLayout emailMessageContainer;

    private Display display;



    @Override
    protected void onCreate(Bundle savedInstanceState)
        {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.activity_test);

        gmailListContainer = (LinearLayout) this.findViewById(R.id.email_activity_gmail_layout);
        yahooMailListContainer = (LinearLayout) this.findViewById(R.id.email_activity_yahoo_mail_layout);
        windowsLiveListContainer = (LinearLayout) this.findViewById(R.id.email_activity_windows_live_layout);
        emailMessageContainer = (LinearLayout) this.findViewById(R.id.email_activity_email_content_layout);

        gmailListContainer.setBackgroundColor(Color.CYAN);
        yahooMailListContainer.setBackgroundColor(Color.MAGENTA);
        windowsLiveListContainer.setBackgroundColor(Color.GREEN);
        emailMessageContainer.setBackgroundColor(Color.RED);

        display = getWindowManager().getDefaultDisplay();

        setMeasuredDimensions();
        }

    private void setMeasuredDimensions()
        {
        View v = this.findViewById(R.id.email_activity_layout);
        v.measure(display.getWidth(), display.getHeight());
        Log.v("EmailActivity", v.getMeasuredHeight() + ", " +v.getMeasuredWidth());
        }

    private void setWeight(LinearLayout container, float weight)
        {
        LinearLayout.LayoutParams params = (LayoutParams) container.getLayoutParams();
        params.weight = weight;
        container.setLayoutParams(params);
        container.setVisibility(View.VISIBLE);
        }

    }
以下是相关的布局资源:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/email_activity_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">

<LinearLayout
    android:id="@+id/email_activity_gmail_layout"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    />
<LinearLayout
    android:id="@+id/email_activity_yahoo_mail_layout"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    />
<LinearLayout
    android:id="@+id/email_activity_windows_live_layout"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    />
<LinearLayout
    android:id="@+id/email_activity_email_content_layout"
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="0"
    />


将以下方法添加到您的活动中,并从中调用您的方法

@Override
public void onWindowFocusChanged(boolean hasFocus) {
      setMeasuredDimensions();
    super.onWindowFocusChanged(hasFocus);
}

在onCreate()中,无法确保视图已绘制。这可能需要一些时间。但是,只有当视图完全绘制时,才会调用上述方法。所以你应该可以在这里精确地得到宽度和高度

请记下我之前的评论。这里有一个经过编辑的:嘿,那里!我尝试了您的解决方案,但是当我调用
v.measure(display.getWidth(),display.getHeight())时,它仍然有效再次,值返回为0。我修改了我的代码,先记录高度和宽度,然后在视图上再次发出测量命令,提供显示器的高度和宽度,然后再次记录高度和宽度。在第一个日志上,返回正确的值。在第二个日志中,值已重置为0。为什么?你检查过显示器的宽度和高度了吗?也许它本身就是0不幸的是,它不是0。此外,如果我错了,请纠正我,但这种方法可以衡量已经得出了观点。我想找到的是一条通往1的路。设置内部线性布局,使前3个权重为1,第4个权重为0.2。强制布局、测量或其他方式,以便我可以在模式3中计算每个人的高度和重量。设置布局,使前3个布局中的2个布局的权重为0,1个布局的权重为1,第4个布局的权重为2 4。在第二种模式5中获取每个模式的宽度和高度。恢复权重,然后我才能绘制视图。这看起来非常巨大。也许你应该把它作为一个单独的问题来问,以便有人能帮助你。