zoukankan      html  css  js  c++  java
  • Android如何在初始化的时候获取加载的布局的宽高

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

    private View headView;
    headView = inflater.inflate(R.layout.header, null);

    如果接下来调用:

    headView.getHeight();
    headView.getMeasuredHeight();

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

    private void measureView(View child) {
    		ViewGroup.LayoutParams lp = child.getLayoutParams();
    		if(lp == null){
    			lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    		}
    		//headerView的宽度信息
    		int childMeasureWidth = ViewGroup.getChildMeasureSpec(0, 0, lp.width);
    		int childMeasureHeight;
    		if(lp.height > 0){
    			childMeasureHeight = MeasureSpec.makeMeasureSpec(lp.height, MeasureSpec.EXACTLY);
    			//最后一个参数表示:适合、匹配
    		} else {
    			childMeasureHeight = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);//未指定
    		}
    //System.out.println("childViewWidth"+childMeasureWidth);
    //System.out.println("childViewHeight"+childMeasureHeight);
    		//将宽和高设置给child
    		child.measure(childMeasureWidth, childMeasureHeight);
    	}

    在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);获取他的宽高。

    View view = mInflater.inflate(R.layout.delete_btn, null);  
            mDelBtn = (Button) view.findViewById(R.id.id_item_btn);  
            mPopupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT,  
                    LinearLayout.LayoutParams.WRAP_CONTENT);  
            /** 
             * 先调用下measure,否则拿不到宽和高 
             */  
            mPopupWindow.getContentView().measure(0, 0);  
            mPopupWindowHeight = mPopupWindow.getContentView().getMeasuredHeight();  
            mPopupWindowWidth = mPopupWindow.getContentView().getMeasuredWidth();

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

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

    setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
                    getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));

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

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

    <View xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="#ffff0000"
             />

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

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
         >
    	<View
            android:layout_width="50dp"
            android:layout_height="50dp"
             />
    </LinearLayout>

    还有一个textview_layout.xml:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="Test"
             />

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/mainLayout"
         >
    </LinearLayout>

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

    inflater = LayoutInflater.from(this);
            mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
            view = inflater.inflate(R.layout.view_layout, null);
            textview = inflater.inflate(R.layout.textview_layout, null);
            viewgroup = inflater.inflate(R.layout.viewgroup_layout, null);
            
            measureView(view);
            measureView(textview);
            measureView(viewgroup);
            
            Log.i(TAG, "view的width和height分别是:" + view.getMeasuredWidth()+ "  " + view.getMeasuredHeight());
            Log.i(TAG, "textview的width和height分别是:" + textview.getMeasuredWidth()+ "  " + textview.getMeasuredHeight());
            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里面。

    mainLayout.addView(view);
    mainLayout.addView(textview);
    mainLayout.addView(viewgroup);
    
        @Override
        public void onWindowFocusChanged(boolean hasFocus) {
        	Log.i(TAG, "获得焦点后");
    //    	Log.i(TAG, "view的width和height分别是:" + view.getMeasuredWidth()+ "  " + view.getHeight());
        	Log.i(TAG, "textview的width和height分别是:" + textview.getMeasuredWidth()+ "  " + textview.getMeasuredHeight());
            Log.i(TAG, "viewgroup的width和height分别是:" + viewgroup.getMeasuredWidth()+ "  " + viewgroup.getMeasuredHeight());
        	super.onWindowFocusChanged(hasFocus);
    }

    打印出来的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中也有类似的方法,和开始的方法基本一样。

    private void measureItem(View child) {  
             ViewGroup.LayoutParams p = child.getLayoutParams();  
             if (p == null) {  
                 p = new ViewGroup.LayoutParams(  
                         ViewGroup.LayoutParams.MATCH_PARENT,  
                         ViewGroup.LayoutParams.WRAP_CONTENT);  
             }  
       
             int childWidthSpec = ViewGroup.getChildMeasureSpec(mWidthMeasureSpec,  
                     mListPadding.left + mListPadding.right, p.width);  
             int lpHeight = p.height;  
             int childHeightSpec;  
             if (lpHeight > 0) {  
                 childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY);  
             } else {  
                 childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);  
             }  
             child.measure(childWidthSpec, childHeightSpec);  
         }  

  • 相关阅读:
    kill all process on a specific port on linux 码农
    rxjs 学习系列二(环境搭建) 码农
    实拍图与摄相头自动合成软件(效果) 码农
    js中for in的用法
    Android sdk资源包res里面的drawable(ldpi、mdpi、hdpi、xhdpi、xxhdpi)
    js中addEventListener中第3个参数
    【agc006f】Blackout
    Trie&可持久化Trie
    【agc014d】Black and White Tree
    【agc009b】Tournament
  • 原文地址:https://www.cnblogs.com/qhyuan1992/p/5385341.html
Copyright © 2011-2022 走看看