measure()后getMeasuredHeight()和width返回0

7
我对安卓中的布局测量非常困惑。基本上,我想在视图被布局之前获取其实际计算出的高度和宽度。我需要获取计算后的高度和宽度,因为我有一个隐藏的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"
    />

1个回答

6
将以下方法添加到您的 Activity 中,并从该方法调用您的方法。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
      setMeasuredDimensions();
    super.onWindowFocusChanged(hasFocus);
}

在onCreate()方法中,您不能保证视图已经绘制完成。这可能需要一些时间。但是,只有当视图完全绘制时,上述方法才会被调用。因此,在此处您应该能够准确地获取宽度和高度。

嘿!我尝试了你的解决方案,它有效,但是当我再次调用v.measure(display.getWidth(), display.getHeight());时,值会重新变为0。我修改了我的代码,首先记录高度和宽度,再次对视图执行测量命令,提供显示器的高度和宽度,并再次记录高度和宽度。在第一次记录中,返回了正确的值。在第二次记录中,这些值已被重置为0。为什么会这样? - Neilers
你检查了你的显示器宽度和高度吗?也许它本身就是0。 - Andro Selva
不幸的是,它不是0。并且,请纠正我如果我错了,但这种方法假设视图已经被绘制了。我试图找到一种方法来 1. 设置内部线性布局,使前3个权重为1,第4个权重为0 2. 强制布局、测量或其他方式,以便我可以在此模式下获取每个的计算高度和权重 3. 设置布局,使前3个布局中的2个权重为0,1个权重为1,第4个权重为2 4. 获取第二种模式下每个的宽度和高度 5. 恢复权重,然后才会绘制视图。 - Neilers
这看起来很复杂。或许你应该把它作为一个单独的问题来询问,这样别人才能帮助你。 - Andro Selva

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接