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

前端之家收集整理的这篇文章主要介绍了android – getMeasuredHeight()和宽度在measure()后返回0前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我真的很困惑 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"
    />

解决方法

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

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

原文链接:https://www.f2er.com/android/314497.html

猜你在找的Android相关文章