gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:58:04 26 4
gpt4 key购买 nike

我真的对 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com