7

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

4

1 回答 1

6

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

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

在 onCreate() 你不能保证你的视图被绘制。这可能需要一些时间。但是只有在完全绘制视图时才会调用上述方法。所以你应该能够在这里准确地得到宽度和高度。

于 2012-08-14T06:57:24.110 回答