android - getMeasuredHeight() 和宽度在 measure() 之后返回 0

标签 android android-layout

我真的对 android 为布局所做的整个测量事情感到困惑。基本上,我想在布局之前获取 View 的实际计算高度和宽度。我需要获得计算出的高度和宽度,因为我有一个隐藏的 LinearLayout,我想在使用 viewpropertyanimator 打开时对其进行动画处理。我需要在动画制作之前将目标宽度(即计算出的宽度)提供给动画师。此布局的宽度是动态的,因为它依赖于其宽度的 android:weight 参数,因此我无法给出静态值。

这是我的 Activity 课:

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"
    />

最佳答案

将以下方法添加到您的 Activity 并从中调用您的方法。

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

在 onCreate() 中,您无法确保您的 View 已绘制。这可能需要一些时间。但是上面的方法只有在 View 被完全绘制时才会被调用。所以你应该能够在这里准确地获得宽度和高度。

关于android - getMeasuredHeight() 和宽度在 measure() 之后返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11946424/

相关文章:

java - 如何在不完成/停止子 Activity 的情况下完成子 Activity 的父 Activity ?

android - FocusChangeListener 不工作

android - 如何在 Android 中的选项卡按钮之间添加空格?

Android原生字体无法制作

Android:什么时候使用layout_margin,什么时候使用weightSum?

android - 如何为Android中的按钮设置样式?

java - 在 Java 中重命名 cwac-touchlist 包时出现问题

android - 使用 MediaCodec 将加密数据解密并解码为 SurfaceTexture

Android recyclerview 分隔符与内部布局 View 对齐

java - 尝试使用 Eclipse 制作我的第一个应用程序时出错