Android在界面初始化过程中如何获取控件宽高

在项目开发中曾经遇到一个bug,需要在一个PopupWindow里获取到里面布局的宽高,但是无论我调用getMeasureHeight()还是getHeigth() ,获取到的值都是0。类似的问题在给listview添加下拉刷新头的时候也遇到过:

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

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

如果接下来调用:

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

熟悉androidUI绘制流程的朋友都知道,此时两个方法都会返回0,原因就是getHeight()方法在layout之后才会赋值,getMeasuredHeight()则是在测量完成后赋值。要在这里取得加载的布局的宽高,需要程序员调用measure测量出该布局的宽高。

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

之前项目为了解决获取不到高度问题,有这样一个方法:


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);
	}

接下来我们分析一下listview的这段代码:
我们在通过LayoutInflater填充header布局,将XML转化为View的时候,是没有设置View的LayoutParams的,就会进入到lp ==null条件中new一个LayoutParams。接着就是获取宽和高的测量规则:

childMeasureWidth:
调用ViewGroup的getChildMeasureSpec传递的第一个参数(父类的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.measure(0, 0);

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

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

但是我们在调用child.measure(0, 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的时候执行下面代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    inflater = LayoutInflater.from(this);

    mainLayout = findViewById(R.id.mainLayout);
    view = inflater.inflate(R.layout.view_layout, null);
    textView = (TextView) inflater.inflate(R.layout.textview_layout, null);
    viewGroup = (ViewGroup) inflater.inflate(R.layout.viewgroup_layout, null);
  
   	measureView(view);
    measureView(textView);
    measureView(viewGroup);
        
    Log.i(TAG, "view的width和height分别是onCreate:" + view.getMeasuredWidth()+ "  " + view.getMeasuredHeight());
    Log.i(TAG, "textview的width和height分别是onCreate:" + textView.getMeasuredWidth()+ "  " + textView.getMeasuredHeight());
    Log.i(TAG, "viewgroup的width和height分别是onCreate:" + viewGroup.getMeasuredWidth()+ "  " + viewGroup.getMeasuredHeight());
    }

得到的结果是:
在这里插入图片描述
View我们看源码,没有重写onMeasure():

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
            getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}

直接通过setMeasuredDimension()把测量值设置了,所以得出来的值会是0
但是TextView和ViewGroup都重写了onMeasure。在ViewGroup 和TextView里面对AT_MOST和UNSPECIFIED的处理方式一样
一直忽视了UNSPECIFIED很少使用,所以没能发现问题。不能惯性的认为onMeasure里面的操作就是调用setMeasuredDimension进行保存宽高的值。在这之前如果是ViewGroup还会具体的根据里面子View的情况进行处理。

既然谈到了View的尺寸,那么在将他们添加到父布局,再获取以下测量宽高,这时的宽高测量要在onWindowFocusChanged里面(onWindowFocusChanged方法里我们可以获取到控件的宽高值):

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inflater = LayoutInflater.from(this);

        mainLayout = findViewById(R.id.mainLayout);
        view = inflater.inflate(R.layout.view_layout, null);
        textView = (TextView) inflater.inflate(R.layout.textview_layout, null);
        viewGroup = (ViewGroup) inflater.inflate(R.layout.viewgroup_layout, null);

 /*       measureView(view);
        measureView(textView);
        measureView(viewGroup);*/

        mainLayout.addView(view);
        mainLayout.addView(textView);
        mainLayout.addView(viewGroup);

      /*  Log.i(TAG, "view的width和height分别是onCreate:" + view.getMeasuredWidth()+ "  " + view.getMeasuredHeight());
        Log.i(TAG, "textview的width和height分别是onCreate:" + textView.getMeasuredWidth()+ "  " + textView.getMeasuredHeight());
        Log.i(TAG, "viewgroup的width和height分别是onCreate:" + viewGroup.getMeasuredWidth()+ "  " + viewGroup.getMeasuredHeight());*/


    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        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());
    }

结果如下:

/MainActivity: view的width和height分别是:1080  2091
/MainActivity: textview的width和height分别是:1080  0
/MainActivity: viewgroup的width和height分别是:1080  0

View的宽高竟然有值了!原因是什么?在调用addView的时候会使试图重新绘制!至于宽和高为什么是这样的,那是因为调用LinearLayout的addview()之后,子view的lp是null,此时会调用generateDefaultLayoutParams返回的宽为match_parent高为wrap_content的lp。最终传测量该View是的onMeasure的参数是AT_MOST和ScreenSize。因为是个View,直接调用setMeasuredDimension设置。然而textview和viewgrounp之所以高度为0,是因为前面的view已经沾满了全屏,如果我们把mainLayout.addView(view)注释掉,得出的结果如下所示:

MainActivity: view的width和height分别是:0  0
MainActivity: textview的width和height分别是:1080  57
MainActivity: viewgroup的width和height分别是:1080  150

那么为什么我们设置了Textview和viewgrounp都是50dp,但是他们的高度却不一样呢?原因在上面也说过了,因为Textview和view都是最外层的,所以我们在addview()的时候,默认会生成宽为match_parent高为wrap_content的lp,此时因为view没有重写onmeasure()做任何处理,就直接设置了,所以换沾满全屏,但是textview却是重写了onMeasure()方法的,感兴趣的朋友可以自己去看一下源码,最终textview宽度是沾满全屏,高度是自适应;而viewgrounp则是刚好是50dp,我的模拟器dip是3,所以是150

从上面的结果来看,我们通过调用measure(0,0)的方式得到的控件宽高值,这个值与传入的父view的测量规则有关,像我们传入为0,实际上是不考略父view的情况下的出来的值(50dp),但是实际上添加到布局的真实宽高可能和这个值不一样(addview()之后)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值