关于getChildMeasureSpec()
来源:互联网 发布:java上传文件夹到ftp 编辑:程序博客网 时间:2024/05/10 20:52
- /**
- * Does the hard part of measureChildren: figuring out the MeasureSpec to
- * pass to a particular child. This method figures out the right MeasureSpec
- * for one dimension (height or width) of one child view.
- *
- * The goal is to combine information from our MeasureSpec with the
- * LayoutParams of the child to get the best possible results. For example,
- * if the this view knows its size (because its MeasureSpec has a mode of
- * EXACTLY), and the child has indicated in its LayoutParams that it wants
- * to be the same size as the parent, the parent should ask the child to
- * layout given an exact size.
- *
- * @param spec The requirements for this view
- * @param padding The padding of this view for the current dimension and
- * margins, if applicable
- * @param childDimension How big the child wants to be in the current
- * dimension
- * @return a MeasureSpec integer for the child
- */
- public static int getChildMeasureSpec(int spec, int padding, int childDimension) {
- int specMode = MeasureSpec.getMode(spec);
- int specSize = MeasureSpec.getSize(spec);
- int size = Math.max(0, specSize - padding);
- int resultSize = 0;
- int resultMode = 0;
- switch (specMode) {
- // Parent has imposed an exact size on us
- case MeasureSpec.EXACTLY:
- if (childDimension >= 0) {
- resultSize = childDimension;
- resultMode = MeasureSpec.EXACTLY;
- } else if (childDimension == LayoutParams.MATCH_PARENT) {
- // Child wants to be our size. So be it.
- resultSize = size;
- resultMode = MeasureSpec.EXACTLY;
- } else if (childDimension == LayoutParams.WRAP_CONTENT) {
- // Child wants to determine its own size. It can't be
- // bigger than us.
- resultSize = size;
- resultMode = MeasureSpec.AT_MOST;
- }
- break;
- // Parent has imposed a maximum size on us
- case MeasureSpec.AT_MOST:
- if (childDimension >= 0) {
- // Child wants a specific size... so be it
- resultSize = childDimension;
- resultMode = MeasureSpec.EXACTLY;
- } else if (childDimension == LayoutParams.MATCH_PARENT) {
- // Child wants to be our size, but our size is not fixed.
- // Constrain child to not be bigger than us.
- resultSize = size;
- resultMode = MeasureSpec.AT_MOST;
- } else if (childDimension == LayoutParams.WRAP_CONTENT) {
- // Child wants to determine its own size. It can't be
- // bigger than us.
- resultSize = size;
- resultMode = MeasureSpec.AT_MOST;
- }
- break;
- // Parent asked to see how big we want to be
- case MeasureSpec.UNSPECIFIED:
- if (childDimension >= 0) {
- // Child wants a specific size... let him have it
- resultSize = childDimension;
- resultMode = MeasureSpec.EXACTLY;
- } else if (childDimension == LayoutParams.MATCH_PARENT) {
- // Child wants to be our size... find out how big it should
- // be
- resultSize = 0;
- resultMode = MeasureSpec.UNSPECIFIED;
- } else if (childDimension == LayoutParams.WRAP_CONTENT) {
- // Child wants to determine its own size.... find out how
- // big it should be
- resultSize = 0;
- resultMode = MeasureSpec.UNSPECIFIED;
- }
- break;
- }
- return MeasureSpec.makeMeasureSpec(resultSize, resultMode);
- }
这个Measure算法主要是算child的测量方式和大小的。主要根据如下四种(下面所指的自身是调用此方法的viewgroup):
1. 自身的测量方式(MeasureSpec EXACTLY,AT_MOST,UNSPECIFIED)【这个是parent传过来的】
2. 自身的大小 【这个是parent传过来的】
3. 自己的padding【计算得到】
4. child的大小【child的LayoutParam得到】
根据以上四个推测出child的MeasureSpec 。也就是大小和测量方式。
child的请求大小来确定到底child应该有多宽以及应该怎么测量。
0 0
- 关于getChildMeasureSpec()
- ViewGroup getChildMeasureSpec
- Android getChildmeasureSpec 方法的分析
- MeasureSpec介绍及使用详解 ViewGroup.getChildMeasureSpec(0,0,0)
- ViewGroup.getChildMeasureSpec(int spec, int padding, int childDimension)解释
- 关于
- 关于
- 关于++,--
- 关于#
- 关于。。。
- 关于
- 关于......
- 关于
- 关于
- 关于****
- 关于
- 关于!
- 关于++
- 数据结构与算法小结(2)
- CentOS 6.3 编译安装 Nginx(含:管理脚本)
- URAL 1113(数学)
- 僵尸进程的产生和避免
- 本博客在2016/02/28被入侵了
- 关于getChildMeasureSpec()
- HashMap深度解析(一)
- Android中的线程
- hdu1561(树形dp)
- 在win10上使用VMware workstation安装ubuntu12.04
- eventlet引发的学习-初识多线程
- IOS学习 ModalViewController模态视图的创建 两视图间用代理方式<传值>
- FFmpeg在Android上的移植优化步骤
- [Python入门]Chapter11 字典