getWidth 得到的事某个View的实际尺寸。

getMeasuredWidth 得到的是某个View想要在parent view里面占的大小

相比你也见过这样的解释,听起来这样的解释也是云里雾里,没有把问题点透

错误的版本不多说了,下面对这两个方法做一下正解,首先大家应先知道一下几点:

1. 在一个类初始化时,即在构造函数当中我们是得不到View的实际大小的。感兴趣的朋友可以试一下,getWidth()和getMeasuredWidth()得到的结果都是0.但是我们可以从onDraw()方法里面的到控件的大小。

2.这两个所得到的结果的单位是像素即pixel。

对这两个方法做介绍:

getWidth(): 得到的是view在父Layout中布局好后的宽度值,如果没有父布局,那么默认的父布局就是真个屏幕。也许不好理解通过一个例子来说明一下:

    public class Test extends Activity {
private LinearLayout mBackgroundLayout;
private TextViewTest mTextViewTest; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); mBackgroundLayout = new MyLayout(this);
mBackgroundLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT)); mTextViewTest = new TextViewTest(this); mBackgroundLayout.addView(mTextViewTest);
setContentView(mBackgroundLayout);
}
public class MyLayout extends LinearLayout{ public MyLayout(Context context) {
super(context);
// TODO Auto-generated constructor stub
} @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
super.onLayout(changed, l, t, r, b);
Log.i("Tag", "--------------");
View mView=getChildAt(0);
mView.measure(0, 0);
} }
public class TextViewTest extends TextView {
public TextViewTest(Context context) {
super(context);
// TODO Auto-generated constructor stub
setText("test test ");
} @Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
// measure(0, 0);
Log.i("Tag", "width: " + getWidth() + ",height: " + getHeight());
Log.i("Tag", "MeasuredWidth: " + getMeasuredWidth()
+ ",MeasuredHeight: " + getMeasuredHeight());
} }
}

这里是在LinearLayout里添加的一个TextView控件,如果此时要得到对TextView获得getWidth(),那么是在TextView添加到Layout后再去获取值,并不单单的是对TextView本身宽度的获取。

getMeasuredWidth():先看一下API里面是怎么说的。

The width of this view as measured in the most recent call to measure(). This should be used during measurement and layout calculations only.

得到的是最近一次调用measure()方法测量后得到的是View的宽度,它仅仅用在测量和Layout的计算中。

所以此方法得到的是View的内容占据的实际宽度。

你如果想从一个简单的例子中得到他们的不同,下面将对上面的例子做一下修改。

    public class Test extends Activity {
private TextViewTest mTextViewTest; /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mTextViewTest = new TextViewTest(this);
setContentView(mTextViewTest);
} public class TextViewTest extends TextView {
public TextViewTest(Context context) {
super(context);
// TODO Auto-generated constructor stub
setText("test test ");
} @Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
measure(0, 0);
Log.i("Tag", "width: " + getWidth() + ",height: " + getHeight());
Log.i("Tag", "MeasuredWidth: " + getMeasuredWidth()
+ ",MeasuredHeight: " + getMeasuredHeight());
}
}
}

总结(正解):

getWidth(): View在设定好布局后整个View的宽度。

getMeasuredWidth(): 对View上的内容进行测量后得到的View内容占据的宽度,前提是你必须在父布局的onLayout()方法或者此View的onDraw()方法里调用measure(0,0);(measure中的参数的值你自己可以定义),否则你得到的结果和getWidth()得到的结果是一样的。

学习的时候来自网上整理转载。供大家学习

Android getWidth和getMeasuredWidth的区别的更多相关文章

  1. Android getWidth和getMeasuredWidth

    1. 在一个类初始化时,即在构造函数当中我们是得不到View的实际大小的.感兴趣的朋友可以试一下,getWidth()和getMeasuredWidth()得到的结果都是0.但是我们可以从onDraw ...

  2. android getWidth()和getMeasuredWidth()方法的区别

    getWidth() Return the width of the your view. Returns The width of your view, in pixels. 源代码: public ...

  3. getWidth()和getMeasuredWidth()的区别

    结论:getMeasuredWidth()获取的是view原始的大小,也就是这个view在XML文件中配置或者是代码中设置的大小.getWidth()获取的是这个view最终显示的大小,这个大小有可能 ...

  4. Android -- getWidth()与getMeasuredWidth()

    getWidth() Return the width of the your view. Returns The width of your view, in pixels. 源代码: public ...

  5. getWidth() 和 getMeasuredWidth()的区别

    getWidth(): View在设定好布局后整个View的宽度.   getMeasuredWidth(): 对View上的内容进行测量后得到的View内容占据的宽度,前提是你必须在父布局的onLa ...

  6. Android中View窗口getWidth和getMeasuredWidth的差别

    今天在研究自己定义listview的下拉刷新的效果.想移植到项目需求中,再看自己定义源代码时发现了一个问题就是getWidth和getMeasuredWidth两个方法有什么差别,求教万能的百度,经调 ...

  7. Android SingleTask与SingleInstance的区别

    Android SingleTask与SingleInstance的区别 原文地址 现有2个项目,taskA.taskB.taskA负责调用taskB中指定的界面. taskB中有3个界面,a.b.c ...

  8. Xamarin.Form与Xamarin.Android或Xamarin.IOS的区别简述

    Xamarin.Form与Xamarin.Android或Xamarin.IOS的区别简述: 可能刚刚接触Xamarin的人来说,对于这个概念比较的模糊,认为这说的不都是同一个东西吗?事实并不是这样的 ...

  9. Android(四)-JVM与DVM区别

    JVM与DVM区别 1.由来 Android是基于java的既然java已经有了java虚拟机,为什么android还要弄一个DVM了?最重要的就是版权问题,一开始就是用的 JVM,没过多久就被SUN ...

随机推荐

  1. zoj 1375 贪心

    https://vjudge.net/problem/ZOJ-1375 In modern day magic shows, passing through walls is very popular ...

  2. mysql备份shell脚步

    #!/bin/bash  #Shell Command For Backup MySQL Database Everyday Automatically By Crontab     USER=roo ...

  3. 《Effective C++》——条款04:确定对象使用前已先被初始化

    读取未初始化的值会导致不明确的行为.在某些平台上,仅仅只是读取未初始化的值,就可能让你的程序终止运行.更可能的情况是读入一些“半随机”bits,污染了正在进行读取动作的那个对象,最终导致不可预知的程序 ...

  4. MySQL+Node.js连接和操作

    在本节中,您将学习如何使用mysql模块从node.js应用程序与MySQL进行交互. 我们将向您展示如何使用Node.js连接到MySQL,执行常用操作,如使用mysql模块API执行插入,选择,更 ...

  5. MySQL5 LOAD DATA 的使用

    MySQL5 LOAD DATA 的使用   数据库中,最常见的写入数据方式是通过SQL INSERT来写入,另外就是通过备份文件恢复数据库,这种备份文件在MySQL中是SQL脚本,实际上执行的还是在 ...

  6. 转载:【菜鸟玩Linux开发】通过MySQL自动同步刷新Redis

    转载: http://www.cnblogs.com/zhxilin/archive/2016/09/30/5923671.html

  7. HAWQ 官方文档创建filespace,tablespace,database,table

    1.创建Filespace 创建Filespace必须是数据库超级用户( You must be a database superuser to create a filespace.)首先创建一个f ...

  8. BZOJ2784: [JLOI2012]时间流逝

    BZOJ2784: [JLOI2012]时间流逝 https://lydsy.com/JudgeOnline/problem.php?id=2784 分析: 挺有意思的一道题. 注意到状态数是\(P( ...

  9. Java邮箱发送——企业版

    企业版邮箱发送工具类 import java.security.Security; import java.util.Properties; import javax.mail.Authenticat ...

  10. 手机访问PC网站自动跳转到手机网站代码(转)

    4G时代,手机网站已经非常普遍了,一般手机网站都有一个二级域名来访问,比如 m.16css.com 如果手机直接访问www.16css.com 就是PC网站,在手机上浏览电脑版网站体验非常不好. 如果 ...