我真的很困惑
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" />