关于getMeasuredHeight和getHeight区别

来源:互联网 发布:淘宝有订单怎么发货 编辑:程序博客网 时间:2024/05/15 12:38

关于getMeasuredHeight和getHeight区别

getMeasuredHeight:是用于测量的高度,也就是View实际的高度(先暂时这么记,后面还有一个显示出来的高度),getMeasuredHeight的值是在onMeasure方法里面通过setMeasuredDimension();设置出来的。也就是说要在onMeasure方法之后调用,不能再之前,这样会得到0。
如果你的View没有设置setMeasuredDimension方法,而是android:layout_height="match_parent"那么你的getMeasuredHeight就等于你手机屏幕的宽度,Log.i("yaoyan"," "+ getMeasuredWidth());这里写图片描述博主手机分辨率还是蛮高的,如果你设置的android:layout_height="match_parent"为200dp,那么打印出来的是像素你可以通过View.getResources().getDisplayMetrics().density*200这样就可以转换为像素了。

getHeight:是指在屏幕上显示出来的高度(这里要强调一点就是不能再onCreate方法里面获得一个View的高度),这个方法得到的是View显示在桌面上的高度(跟前面测量的高度相对对应),因为View的onMeasure方法运行完之后还会运行一个onLayout方法,要等到onLayout运行完之后才能得到具体的值,这个方法是指View所在屏幕上的位置,通过View.layout(int Left,int Top,int Right,int Bottom)改变View在屏幕上的大小(这个方法只是改变形状上的大小,实际的View并没有改变)
下面来看代码
布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/RL"    android:layout_width="match_parent"    android:layout_height="match_parent"    ><com.xie.acer1.test.youyou    android:id="@+id/youyou"    android:background="#000000"    android:layout_width="300dp"    android:layout_height="300dp" />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="New Button"        android:id="@+id/button"        android:onClick="Button"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:layout_alignParentStart="true" /></RelativeLayout>

自定义View文件

package com.xie.acer1.test;import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;import android.util.Log;import android.view.View;/** * Created by acer1 on 2016/1/23. */public class youyou extends View {    public youyou(Context context) {        super(context);    }    public youyou(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onDraw(Canvas canvas) {        Log.i("yaoyan","   "+ getMeasuredHeight()+"  "+getHeight());        super.onDraw(canvas);    }}
package com.xie.acer1.test;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;public class MainActivity extends AppCompatActivity {    private youyou y;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        y = (youyou) findViewById(R.id.youyou);    }    public void Button(View view){        y = (youyou) findViewById(R.id.youyou);        y.layout(y.getLeft(),y.getTop(),y.getRight(),y.getBottom()-10);        System.out.println(y.getTop());    }}这里在强调一点getLeft是设置View距离父View的距离,如果一个布局文件中有很多的LinearLayout或者RelativeLayout,一圈一圈的嵌套,容易搞混,一定是距离父窗体

这里写图片描述
这里写图片描述
这里写图片描述
这里可以看到每次点击Button按钮,View视图在距离底部在一点点的缩小,getHeight也越来越小,但是getMeasuredHeight却始终都没有变化,这就是实际测量视图的高度与手机可视高度的区别

注意看第一张图片,博主没有点击Button的时候getHeight和getMeasuredHeight是一样的。

也有很多初学者对getHeight和getMeasuredHeight的值为什么一样苦恼
可以看一下源码,究竟有什么区别

/**     * Return the height of your view.     *     * @return The height of your view, in pixels.     */    @ViewDebug.ExportedProperty(category = "layout")    public final int getHeight() {        return mBottom - mTop;    }

如果我们不特意设置View.layout方法,getHeight所得到的高度就是getMeasuredHeight实际高度,所以有的时候这两个值是一样的。

博主第一篇技术博客,写的不好请大家指出来,轻点喷。

2 0
原创粉丝点击