[置顶] 关于getMeasuredHeight和getHeight区别
2016-01-23 23:06
429 查看
关于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实际高度,所以有的时候这两个值是一样的。
博主第一篇技术博客,写的不好请大家指出来,轻点喷。
相关文章推荐
- nodejs package开发速查
- Linux常用命令_进程管理
- java学习之字符流与字节流的转换
- CodeForces 609C-C. Load Balancing
- Android异步加载访问网络图片-解析json
- centos编译安装PHP
- 分享一个使用NSIS制作安装包的UI插件
- java学习之IO字符流
- android MediaRecorder录制视频(可暂停、继续)
- 一些很实用但是JavaScript没有提供的函数
- 商品简介字数限制解决方法
- LTP 源码分析- 总纲
- 代码:swust oj 0083 阶乘问题
- 阴影,图层蒙板及圆角(当和maskToBounds一起使用时)影响TableView的性能
- s:select
- 第一天了解到了html5 + 像素的含义
- 求解数独的C++实现
- WebUploader asp.net 多文件上传
- 深度学习在腾讯的平台化和应用实践
- Vitamio库打造万能播放器(三)