MeasureSpec介绍及使用详解

论坛 期权论坛     
选择匿名的用户   2021-5-23 19:53   159   0
<p style="margin-top:10px; margin-right:auto; margin-left:auto; margin-bottom:0cm; font-family:Verdana,&#39;Lucida Grande&#39;,Arial,Helvetica,sans-serif; line-height:18px"> 一个<span style="font-family:&#39;Times New Roman&#39;,serif">MeasureSpec</span>封装了父布局传递给子布局的布局要求,每个<span style="font-family:&#39;Times New Roman&#39;,serif">MeasureSpec</span>代表了一组宽度和高度的要求。一个<span style="font-family:&#39;Times New Roman&#39;,serif">MeasureSpec</span>由大小和模式组成。它有三种模式:<span style="font-family:&#39;Times New Roman&#39;,serif">UNSPECIFIED(</span>未指定<span style="font-family:&#39;Times New Roman&#39;,serif">),</span>父元素部队自元素施加任何束缚,子元素可以得到任意想要的大小;<span style="font-family:&#39;Times New Roman&#39;,serif">EXACTLY(</span>完全<span style="font-family:&#39;Times New Roman&#39;,serif">)</span>,父元素决定自元素的确切大小,子元素将被限定在给定的边界里而忽略它本身大小;<span style="font-family:&#39;Times New Roman&#39;,serif">AT_MOST(</span>至多<span style="font-family:&#39;Times New Roman&#39;,serif">)</span>,子元素至多达到指定大小的值。</p>
<p style="margin-top:10px; margin-right:auto; margin-left:auto; margin-bottom:0cm; font-family:Verdana,&#39;Lucida Grande&#39;,Arial,Helvetica,sans-serif; line-height:18px">  </p>
<p style="margin-top:10px; margin-right:auto; margin-left:auto; margin-bottom:0cm; font-family:Verdana,&#39;Lucida Grande&#39;,Arial,Helvetica,sans-serif; line-height:18px">   它常用的三个函数:</p>
<p style="margin-top:10px; margin-right:auto; margin-left:auto; margin-bottom:0cm; font-family:Verdana,&#39;Lucida Grande&#39;,Arial,Helvetica,sans-serif; line-height:18px"> <span style="font-family:&#39;Times New Roman&#39;,serif">  1.static int getMode(int measureSpec):</span>根据提供的测量值<span style="font-family:&#39;Times New Roman&#39;,serif">(</span>格式<span style="font-family:&#39;Times New Roman&#39;,serif">)</span>提取模式<span style="font-family:&#39;Times New Roman&#39;,serif">(</span>上述三个模式之一<span style="font-family:&#39;Times New Roman&#39;,serif">)</span></p>
<p style="margin-top:10px; margin-right:auto; margin-left:auto; margin-bottom:0cm; font-family:Verdana,&#39;Lucida Grande&#39;,Arial,Helvetica,sans-serif; line-height:18px"> <span style="font-family:&#39;Times New Roman&#39;,serif">  2.static int getSize(int measureSpec):</span>根据提供的测量值<span style="font-family:&#39;Times New Roman&#39;,serif">(</span>格式<span style="font-family:&#39;Times New Roman&#39;,serif">)</span>提取大小值<span style="font-family:&#39;Times New Roman&#39;,serif">(</span>这个大小也就是我们通常所说的大小<span style="font-family:&#39;Times New Roman&#39;,serif">)</span></p>
<p style="margin-top:10px; margin-right:auto; margin-left:auto; margin-bottom:0cm; font-family:Verdana,&#39;Lucida Grande&#39;,Arial,Helvetica,sans-serif; line-height:18px"> <span style="font-family:&#39;Times New Roman&#39;,serif">  3.static int makeMeasureSpec(int size,int mode):</span>根据提供的大小值和模式创建一个测量值<span style="font-family:&#39;Times New Roman&#39;,serif">(</span>格式<span style="font-family:&#39;Times New Roman&#39;,serif">)</span></p>
<p style="margin-top:10px; margin-right:auto; margin-left:auto; margin-bottom:0cm; font-family:Verdana,&#39;Lucida Grande&#39;,Arial,Helvetica,sans-serif; line-height:18px"> <span style="font-family:&#39;Times New Roman&#39;,serif"><br> </span></p>
<p style="margin-top:10px; margin-right:auto; margin-left:auto; margin-bottom:0cm; font-family:Verdana,&#39;Lucida Grande&#39;,Arial,Helvetica,sans-serif; line-height:18px">   这个类的使用呢,通常在<span style="font-family:&#39;Times New Roman&#39;,serif">view</span>组件的<span style="font-family:&#39;Times New Roman&#39;,serif">onMeasure</span>方法里面调用但也有少数例外,看看几个例子:</p>
<p style="margin-top:10px; margin-right:auto; margin-left:auto; margin-bottom:0cm; font-family:Verdana,&#39;Lucida Grande&#39;,Arial,Helvetica,sans-serif; line-height:18px">  </p>
<p style="margin-top:10px; margin-right:auto; margin-left:auto; margin-bottom:0cm; font-family:Verdana,&#39;Lucida Grande&#39;,Arial,Helvetica,sans-serif; line-height:18px">   a.首先一个我们常用到的一个有用的函数,<span style="font-family:&#39;Times New Roman&#39;,serif">View.resolveSize(int size,int measureSpec)</span></p>
<p style="margin-top:10px; margin-right:auto; margin-left:auto; margin-bottom:0cm; font-family:Verdana,&#39;Lucida Grande&#39;,Arial,Helvetica,sans-serif; line-height:18px">  </p>
<div class="cnblogs_code" style="font-family:&#39;Courier New&#39;; border-top-width:1px; border-right-width:1px; border-bottom-width:1px; border-left-width:1px; border-top-style:solid; border-right-style:solid; border-bottom-style:solid; border-left-style:solid; border-top-color:rgb(204,204,204); border-right-color:rgb(204,204,204); border-bottom-color:rgb(204,204,204); border-left-color:rgb(204,204,204); padding-top:5px; padding-right:5px; padding-bottom:5px; padding-left:5px; overflow-x:auto; overflow-y:auto; margin-top:5px; margin-right:0px; margin-bottom:5px; margin-left:0px; max-width:900px; line-height:18px">
<div class="cnblogs_code_toolbar" st
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP