Android如何在初始化的时候获取加载的布局的宽高

来源:互联网 发布:淘宝手机版联系客服 编辑:程序博客网 时间:2024/06/06 12:27

在自定义ListView中,需要将下拉刷新的View在初始化的时候设置padding隐藏起来,这时就要在初始化的时候获得要加载的布局View的高度。

[java] view plaincopy
  1. private View headView;  
  2. headView = inflater.inflate(R.layout.header, null);  

如果接下来调用:

[java] view plaincopy
  1. headView.getHeight();  
  2. headView.getMeasuredHeight();  

我们知道都会返回0,原因是getMeasuredHeight要在measure后才有值,getHeight要在layout后才有值。要在这里取得加载的布局的宽高,需要程序员调用measure测量出该布局的宽高。以前在写下拉ListView的时候有这样一段代码,一直不太明白,今天研究了一下:

[java] view plaincopy
  1. private void measureView(View child) {  
  2.         ViewGroup.LayoutParams lp = child.getLayoutParams();  
  3.         if(lp == null){  
  4.             lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);  
  5.         }  
  6.         //headerView的宽度信息  
  7.         int childMeasureWidth = ViewGroup.getChildMeasureSpec(00, lp.width);  
  8.         int childMeasureHeight;  
  9.         if(lp.height > 0){  
  10.             childMeasureHeight = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);  
  11.             //最后一个参数表示:适合、匹配  
  12.         } else {  
  13.             childMeasureHeight = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);//未指定  
  14.         }  
  15. //System.out.println("childViewWidth"+childMeasureWidth);  
  16. //System.out.println("childViewHeight"+childMeasureHeight);  
  17.         //将宽和高设置给child  
  18.         child.measure(childMeasureWidth, childMeasureHeight);  
  19.     }  

在inflate完布局后,调用measureView(headView),然后再调用getMeasureHeight函数就可以获得实际高度了。

 

接下来从原理上分析一下这段代码:

通过inflater.inflate(R.layout.header, null);将XML转化为View的时候,是没有设置View的LayoutParams的,就会进入到lp ==null条件中new一个LayoutParams。接下来设置View的childMeasureWidth和childMeasureHeight这点比较费解。

childMeasureWidth:

调用ViewGroupgetChildMeasureSpec传递的第一个参数(父类的spec)竟然是0,传进0表示父类的spec是未指定的MeasureSpec.UNSPECIFIED模式,size也为0。那么该函数返回的值就是一个UNSPECIFIED类型size为0的值。

childMeasureHeight:

和上面类似,如果lp.height > 0就表示设置的是一个精确值,接下来会封装一个EXACTLY和lp.height的值给childMeasureHeight,然后这里lp.height是wrap_content,他是个负值(-2),所以会封装一个未指定UNSPECIFIED和size为0的值给childMeasureHeight。

即childMeasureWidth和childMeasureHeight两个参数均为0。

关于传递给child的widthMeasureSpec和heightMeasureSpec可以通过打印出的结果看出来确实是这样的。

然后将这两个值作为参数传递给子View的measure函数。

上次在张鸿洋大神的博客看到在划动删除代码里这样写,获取PopupWindow的宽高之前调用了mPopupWindow.getContentView().measure(0, 0);获取他的宽高。

[java] view plaincopy
  1. View view = mInflater.inflate(R.layout.delete_btn, null);    
  2.         mDelBtn = (Button) view.findViewById(R.id.id_item_btn);    
  3.         mPopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT,    
  4.                 LinearLayout.LayoutParams.WRAP_CONTENT);    
  5.         /**  
  6.          * 先调用下measure,否则拿不到宽和高  
  7.          */    
  8.         mPopupWindow.getContentView().measure(00);    
  9.         mPopupWindowHeight = mPopupWindow.getContentView().getMeasuredHeight();    
  10.         mPopupWindowWidth = mPopupWindow.getContentView().getMeasuredWidth();  

这样看来,其实和一开始给的代码是一样的。因为分析完开始的代码,我们发现最后执行child.measure(childMeasureWidth, childMeasureHeight);的时候两个参数就是0。

我最初很难理解父布局传递给子布局一个UNSPECIFIED和0的值。因为默认情况下对于View来说会直接设置

[java] view plaincopy
  1. setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),  
  2.                 getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));  

里面的函数前面都学习过,很明显会将传过来的0设置给该View。但是调用这段代码后却是得到了真事的尺寸不是0。

带着问题,我将测量的布局仅仅写一个View,命名为view_layout.xml:

[html] view plaincopy
  1. <View xmlns:android="http://schemas.android.com/apk/res/android"  
  2.         android:layout_width="50dp"  
  3.         android:layout_height="50dp"  
  4.         android:background="#ffff0000"  
  5.          />  

还有一个View被LinearLayout包裹着,viewgroup_layout.xml:

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.      >  
  6.     <View  
  7.         android:layout_width="50dp"  
  8.         android:layout_height="50dp"  
  9.          />  
  10. </LinearLayout>  

还有一个textview_layout.xml:

[html] view plaincopy
  1. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  2.         android:layout_width="50dp"  
  3.         android:layout_height="50dp"  
  4.         android:text="Test"  
  5.          />  

activity_main.xml

[html] view plaincopy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:id="@+id/mainLayout"  
  7.      >  
  8. </LinearLayout>  

然后在onCerate的时候执行下面代码:

[java] view plaincopy
  1. inflater = LayoutInflater.from(this);  
  2.         mainLayout = (LinearLayout) findViewById(R.id.mainLayout);  
  3.         view = inflater.inflate(R.layout.view_layout, null);  
  4.         textview = inflater.inflate(R.layout.textview_layout, null);  
  5.         viewgroup = inflater.inflate(R.layout.viewgroup_layout, null);  
  6.           
  7.         measureView(view);  
  8.         measureView(textview);  
  9.         measureView(viewgroup);  
  10.           
  11.         Log.i(TAG, "view的width和height分别是:" + view.getMeasuredWidth()+ "  " + view.getMeasuredHeight());  
  12.         Log.i(TAG, "textview的width和height分别是:" + textview.getMeasuredWidth()+ "  " + textview.getMeasuredHeight());  
  13.         Log.i(TAG, "viewgroup的width和height分别是:" + viewgroup.getMeasuredWidth()+ "  " + viewgroup.getMeasuredHeight());  

得到的结果是:


果然和最初解释的一样。对于View来说默认情况下会直接设置。

对于继承自View的TextView和ViewGroup而言却获得了其测量值。

原因在哪里?因为TextView和ViewGroup都重写了onMeasure。在ViewGroup 和TextView里面对AT_MOST和UNSPECIFIED的处理方式一样。

一直忽视了UNSPECIFIED很少使用,所以没能发现问题。不能惯性的认为onMeasure里面的操作就是调用setMeasuredDimension进行保存宽高的值。在这之前如果是ViewGroup还会具体的根据里面子View的情况进行处理。

既然谈到了View的尺寸,那么在将他们添加到父布局,再获取以下测量宽高,这时的宽高测量要在onWindowFocusChanged里面。

[java] view plaincopy
  1. mainLayout.addView(view);  
  2. mainLayout.addView(textview);  
  3. mainLayout.addView(viewgroup);  
  4.   
  5.     @Override  
  6.     public void onWindowFocusChanged(boolean hasFocus) {  
  7.         Log.i(TAG, "获得焦点后");  
  8. //      Log.i(TAG, "view的width和height分别是:" + view.getMeasuredWidth()+ "  " + view.getHeight());  
  9.         Log.i(TAG, "textview的width和height分别是:" + textview.getMeasuredWidth()+ "  " + textview.getMeasuredHeight());  
  10.         Log.i(TAG, "viewgroup的width和height分别是:" + viewgroup.getMeasuredWidth()+ "  " + viewgroup.getMeasuredHeight());  
  11.         super.onWindowFocusChanged(hasFocus);  
  12. }  

打印出来的Log如下:


View的宽高竟然有值了!原因是什么?在调用addView的时候会使试图重新绘制!至于宽和高为什么是这样的,那是因为调用generateDefaultLayoutParams返回的宽为match_parent高为wrap_content。最终传测量该View是的onMeasure的参数是AT_MOST和ScreenSize。因为是个View,直接调用setMeasuredDimension设置。

后面两个出现0的原因是View将屏幕占满了。如果不调用mainLayout.addView(view);可以看到:


改成"horizontal"后,打印信息如下:


至于高度,textview为15,由于是最外层,最终为包裹内容。Viewgroup的最终宽高为50dp,由于我的手机像素密度为0.75,因此换算为像素为38,结果一致。

这就告诉我,自己调用onMeasure只是暂时拿到了View的宽高测量值,这个值与传入的父布局的widthMeasureSpec, heightMeasureSpec有关。实际上添加到布局的真实宽高可能和这个值不一样。

ListView中也有类似的方法,和开始的方法基本一样。

[java] view plaincopy
  1. private void measureItem(View child) {    
  2.          ViewGroup.LayoutParams p = child.getLayoutParams();    
  3.          if (p == null) {    
  4.              p = new ViewGroup.LayoutParams(    
  5.                      ViewGroup.LayoutParams.MATCH_PARENT,    
  6.                      ViewGroup.LayoutParams.WRAP_CONTENT);    
  7.          }    
  8.      
  9.          int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,    
  10.                  mListPadding.left + mListPadding.right, p.width);    
  11.          int lpHeight = p.height;    
  12.          int childHeightSpec;    
  13.          if (lpHeight > 0) {    
  14.              childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);    
  15.          } else {    
  16.              childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);    
  17.          }    
  18.          child.measure(childWidthSpec, childHeightSpec);    
  19.      }    
0 0
原创粉丝点击