权威的方式来覆盖onMeasure()? [英] Authorative way to override onMeasure()?

查看:121
本文介绍了权威的方式来覆盖onMeasure()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是压倒一切onMeasure的正确方法()?我见过的各种方法。例如,专业的Andr​​oid开发的使用MeasureSpec计算维度,然后通过调用setMeasuredDimension结束()。例如:

What's the correct way of overriding onMeasure()? I've seen various approaches. For example, Professional Android Development uses MeasureSpec to calculate the dimensions, then ends with a call to setMeasuredDimension(). For example:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth/2, parentHeight);
}

在另一方面,作为每<一个href="http://stackoverflow.com/questions/2159320/how-to-size-an-android-view-based-on-its-parents-dimensions">this帖子,正确的方法是使用MeasureSpec,调用setMeasuredDimensions(),之后调用setLayoutParams(),并调用结局super.onMeasure()。例如:

On the other hand, as per this post, the "correct" way is to use MeasureSpec, call setMeasuredDimensions(), followed by a call to setLayoutParams(), and ending with a call to super.onMeasure(). For example:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
this.setMeasuredDimension(parentWidth/2, parentHeight);
this.setLayoutParams(new *ParentLayoutType*.LayoutParams(parentWidth/2,parentHeight));
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

因此​​,这是正确的方式?这两种方法都已经为我工作100%。

So which is the right way? Neither approach has worked 100% for me.

我想真的是我问的是有没有人知道一个教程,说明onMeasure(),布局,子视图等维度?

I guess really what I'm asking is does anyone know of a tutorial that explains onMeasure(), layout, dimensions of child views etc.?

推荐答案

其他的解决方案是不是COM prehensive。他们可以工作在某些情况下,是一个良好的开端,但他们可能都不能保证工作。

The other solutions are not comprehensive. They may work in some cases, and are a good place to start, but they may are not guaranteed to work.

在onMeasure被调用,你可能会或可能不会有改变大小的权利。传递给你的onMeasure的值( widthMeasureSpec heightMeasureSpec )包含有关你的孩子视图允许做信息。目前有三个值:

When onMeasure gets called you may or may not have the rights to change the size. The values that are passed to your onMeasure (widthMeasureSpec, heightMeasureSpec) contain information about what your child view is allowed to do. Currently there are three values:

  1. MeasureSpec.UNSPECIFIED - 你可以像大的,只要你愿意
  2. MeasureSpec.AT_MOST - 大如你想(达到规格尺寸),这是上级宽度的你的榜样。
  3. MeasureSpec.EXACTLY - 别无选择。家长选择了。
  1. MeasureSpec.UNSPECIFIED - You can be as big as you'd like
  2. MeasureSpec.AT_MOST - As big as you want (up to the spec size), This is parentWidth in your example.
  3. MeasureSpec.EXACTLY - No choice. Parent has chosen.

这是这样做的机器人可以进行多次传递找到合适的大小,每个项目,看的这里了解更多详情。

This is done so that Android can make multiple passes to find the right size for each item, see here for more details.

如果你不遵循这些规则,你的做法是不能保证工作。

例如,如果您要检查,如果你允许更改大小,在所有你能做到以下几点:

For example if you want to check if you're allowed to change the size at all you can do the following:

final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
final int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
boolean resizeWidth = widthSpecMode != MeasureSpec.EXACTLY;
boolean resizeHeight = heightSpecMode != MeasureSpec.EXACTLY;

使用这些信息,你就会知道你是否可以修改的值在code。或者,如果你需要做不同的事情。一个快速简便的方法来解决你想要的大小是使用以下方法之一:

Using this information you will know whether you can modify the values as in your code. Or if you are required to do something different. A quick and easy way to resolve your desired size is to use one of the following methods:

<一个href="http://developer.android.com/reference/android/view/View.html#resolveSizeAndState%28int,%20int,%20int%29">int resolveSizeAndState(INT大小,诠释measureSpec,INT childMeasuredState)

INT resolveSize(INT大小,诠释measureSpec)

在第一个仅适用于蜂窝,二是适用于所有版本。

While the first is only available on Honeycomb, the second is available on all versions.

请注意:您可能会发现, resizeWidth resizeHeight 始终是假的。我发现这是事实,如果我被要求 MATCH_PARENT 。我可以通过要求解决这个问题 WRAP_CONTENT 在我父母的布局,然后在不定的阶段,要求 Integer.MAX_VALUE的。这样做可以给你最大大小的家长可通过onMeasure下传。

Note: You may find that resizeWidth or resizeHeight are always false. I found this to be the case if I was requesting MATCH_PARENT. I was able to fix this by requesting WRAP_CONTENT on my parent layout and then during the UNSPECIFIED phase requesting a size of Integer.MAX_VALUE. Doing so gives you the max size your parent allows on the next pass through onMeasure.

这篇关于权威的方式来覆盖onMeasure()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆