7
我真的很困惑这整个测量东西,android的布局。基本上,我想在布局之前得到实际计算出的视图的高度和宽度。我需要计算高度和宽度,因为我有一个隐藏的LinearLayout
,我想在使用viewpropertyanimator打开时设置动画。我需要在动画设置动画之前提供目标宽度(这是计算的宽度)。这个布局的宽度是动态的,因为它依赖于其宽度的android:weight
参数,所以我不能给出一个静态值。getMeasuredHeight()和宽度返回0后measure()
这里是我的活动类:
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"
/>
划伤我以前的评论。这是一个编辑的: 嘿!我试过你的解决方案,但它的工作原理,但是当我再次调用'v.measure(display.getWidth(),display.getHeight());'时,这些值返回为0.我修改了我的代码来记录高度和宽度,再次在视图上触发一个度量命令,再次提供显示的高度和宽度,并记录高度和宽度。在第一个日志中,返回正确的值。在第二个日志中,值已重置为0.为什么? – Neilers 2012-08-14 08:08:29
你检查了你的显示器的宽度和高度吗?也许它本身就是0 – 2012-08-14 08:39:47
不幸的是,它不是0.另外,纠正我,如果我错了,但这种方法假设,已经绘制的意见。我试图找到的是一种** 1的方法**设置内部线性布局,使得前3个的weight = 1,第4个的weight = 0 ** 2。**强制布局,测量或这样我就可以在这种模式下得到每个人的计算身高和体重** 3。**设置布局,使前3个布局中的2个体重= 0,1体重= 1,第4个体重= 2 ** 4 **在第二种模式下获得每个人的宽度和高度** 5 ** **恢复重量,然后我才能绘制视图 – Neilers 2012-08-14 09:18:45