国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

解析:繼承ViewGroup后的子類如何重寫onMeasure方法

2020-04-11 12:11:32
字體:
來源:轉載
供稿:網友
1.首先貼上我試驗成功的代碼
復制代碼 代碼如下:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

         int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
         int measureHeigth = MeasureSpec.getSize(heightMeasureSpec);
         setMeasuredDimension(measureWidth, measureHeigth);
         // TODO Auto-generated method stub
         for(int i= 0;i<getChildCount();i++){
             View v = getChildAt(i);
             Log.v(TAG, "measureWidth is " +v.getMeasuredWidth() + "measureHeight is "+v.getMeasuredHeight());
             int widthSpec = 0;
             int heightSpec = 0;
             LayoutParams params = v.getLayoutParams();
             if(params.width > 0){
                 widthSpec = MeasureSpec.makeMeasureSpec(params.width, MeasureSpec.EXACTLY);
             }else if (params.width == -1) {
                 widthSpec = MeasureSpec.makeMeasureSpec(measureWidth, MeasureSpec.EXACTLY);
             } else if (params.width == -2) {
                 widthSpec = MeasureSpec.makeMeasureSpec(measureWidth, MeasureSpec.AT_MOST);
             }

             if(params.height > 0){
                 heightSpec = MeasureSpec.makeMeasureSpec(params.height, MeasureSpec.EXACTLY);
             }else if (params.height == -1) {
                 heightSpec = MeasureSpec.makeMeasureSpec(measureHeigth, MeasureSpec.EXACTLY);
             } else if (params.height == -2) {
                 heightSpec = MeasureSpec.makeMeasureSpec(measureWidth, MeasureSpec.AT_MOST);
             }
             v.measure(widthSpec, heightSpec);

         }
     }

解釋一下:
首先判斷params.width的值是多少,有三種情況。
如果是大于零的話,及傳遞的就是一個具體的值,那么,構造MeasupreSpec的時候可以直接用EXACTLY。
如果為-1的話,就是MatchParent的情況,那么,獲得父View的寬度,再用EXACTLY來構造MeasureSpec。
如果為-2的話,就是wrapContent的情況,那么,構造MeasureSpec的話直接用一個負數就可以了。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 花莲市| 盐源县| 海安县| 门头沟区| 富阳市| 盐源县| 华阴市| 马山县| 临湘市| 丹寨县| 黄山市| 资溪县| 象州县| 中卫市| 吉林市| 福州市| 呈贡县| 胶州市| 平泉县| 安宁市| 缙云县| 洛隆县| 连城县| 张掖市| 图们市| 富民县| 山西省| 乌恰县| 宁河县| 滦平县| 玉田县| 乌拉特中旗| 澎湖县| 临漳县| 久治县| 长沙市| 富宁县| 安福县| 深州市| 保德县| 巴彦淖尔市|