zoukankan      html  css  js  c++  java
  • MeasureSpec 的三中类型

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >
    
        <!--  -->
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <com.example.test.MeasTestLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </com.example.test.MeasTestLayout>
        </FrameLayout>
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <com.example.test.MeasTestLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </com.example.test.MeasTestLayout>
        </FrameLayout>
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <com.example.test.MeasTestLayout
                android:layout_width="match_parent"
                android:layout_height="100dp" >
            </com.example.test.MeasTestLayout>
        </FrameLayout>
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <com.example.test.MeasTestLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </com.example.test.MeasTestLayout>
        </FrameLayout>
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <com.example.test.MeasTestLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </com.example.test.MeasTestLayout>
        </FrameLayout>
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
    
            <com.example.test.MeasTestLayout
                android:layout_width="match_parent"
                android:layout_height="100dp" >
            </com.example.test.MeasTestLayout>
        </FrameLayout>
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="100dp" >
    
            <com.example.test.MeasTestLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </com.example.test.MeasTestLayout>
        </FrameLayout>
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="100dp" >
    
            <com.example.test.MeasTestLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </com.example.test.MeasTestLayout>
        </FrameLayout>
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="100dp" >
    
            <com.example.test.MeasTestLayout
                android:layout_width="match_parent"
                android:layout_height="50dp" >
            </com.example.test.MeasTestLayout>
        </FrameLayout>
    
    </FrameLayout>
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // TODO Auto-generated method stub
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);        
            
            print(widthMeasureSpec, heightMeasureSpec);
        }
        
        private void print(int widthMeasureSpec, int heightMeasureSpec){
            int widthMode    = MeasureSpec.getMode(widthMeasureSpec);
            int heightMode    = MeasureSpec.getMode(heightMeasureSpec);
            log("begin>>>");
            log("width    -- mode: "+getMode(widthMode) +" size: "+MeasureSpec.getSize(widthMeasureSpec));
            log("height    -- mode: "+getMode(heightMode) +" size: "+MeasureSpec.getSize(heightMeasureSpec));
            log("<<<");
        }
    11-16 15:19:27.155: D/View(8127): measure: begin>>>
    11-16 15:19:27.155: D/View(8127): measure: width    -- mode: EXACTLY size: 1080
    11-16 15:19:27.155: D/View(8127): measure: height    -- mode: AT_MOST size: 1599->1467
    11-16 15:19:27.155: D/View(8127): measure: <<<
    11-16 15:19:27.155: D/View(8127): measure: begin>>>
    11-16 15:19:27.155: D/View(8127): measure: width    -- mode: EXACTLY size: 1080
    11-16 15:19:27.155: D/View(8127): measure: height    -- mode: AT_MOST size: 1599->1467
    11-16 15:19:27.155: D/View(8127): measure: <<<
    11-16 15:19:27.155: D/View(8127): measure: begin>>>
    11-16 15:19:27.155: D/View(8127): measure: width    -- mode: EXACTLY size: 1080
    11-16 15:19:27.155: D/View(8127): measure: height    -- mode: EXACTLY size: 275
    11-16 15:19:27.155: D/View(8127): measure: <<<
    11-16 15:19:27.155: D/View(8127): measure: begin>>>
    11-16 15:19:27.155: D/View(8127): measure: width    -- mode: EXACTLY size: 1080
    11-16 15:19:27.155: D/View(8127): measure: height    -- mode: EXACTLY size: 1599->1467
    11-16 15:19:27.155: D/View(8127): measure: <<<
    11-16 15:19:27.155: D/View(8127): measure: begin>>>
    11-16 15:19:27.155: D/View(8127): measure: width    -- mode: EXACTLY size: 1080
    11-16 15:19:27.155: D/View(8127): measure: height    -- mode: AT_MOST size: 1599->1467
    11-16 15:19:27.155: D/View(8127): measure: <<<
    11-16 15:19:27.155: D/View(8127): measure: begin>>>
    11-16 15:19:27.155: D/View(8127): measure: width    -- mode: EXACTLY size: 1080
    11-16 15:19:27.155: D/View(8127): measure: height    -- mode: EXACTLY size: 275
    11-16 15:19:27.155: D/View(8127): measure: <<<
    11-16 15:19:27.155: D/View(8127): measure: begin>>>
    11-16 15:19:27.155: D/View(8127): measure: width    -- mode: EXACTLY size: 1080
    11-16 15:19:27.155: D/View(8127): measure: height    -- mode: EXACTLY size: 275
    11-16 15:19:27.155: D/View(8127): measure: <<<
    11-16 15:19:27.155: D/View(8127): measure: begin>>>
    11-16 15:19:27.155: D/View(8127): measure: width    -- mode: EXACTLY size: 1080
    11-16 15:19:27.155: D/View(8127): measure: height    -- mode: AT_MOST size: 275
    11-16 15:19:27.155: D/View(8127): measure: <<<
    11-16 15:19:27.155: D/View(8127): measure: begin>>>
    11-16 15:19:27.155: D/View(8127): measure: width    -- mode: EXACTLY size: 1080
    11-16 15:19:27.155: D/View(8127): measure: height    -- mode: EXACTLY size: 138
    11-16 15:19:27.155: D/View(8127): measure: <<<
    
    结论:1、自定义控件中的onMeasure方法接收的参数的Mode与它的父控件的w、h以及它自身的宽高有关
    2、如果自定义控件自身的高度为固定的dp值,传入的参数的mode一定是EXACTLY
    3、如果自定义控件自身的高度为wrap_content,传入的参数的mode一定是AT_MOST
    4、如果自定义控件自身的高度为match_parent,其父控件的高度为固定dp值,传入的参数的mode一定是EXACTLY
    5、
    如果自定义控件自身的高度为match_parent,其父控件的高度为wrap_content,传入的参数的mode一定是AT_MOST
    6、如果自定义控件自身的高度为match_parent,其父控件的高度为match_parent,传入的参数的mode依赖更上层的父控件,并且可能在多次onMeasure中变化
    7、MeasureSpec.UNSPECIFIED是未指定尺寸,这种情况不多,一般都是父控件是AdapterView,通过measure方法传入的模式。
  • 相关阅读:
    Linux shell(3)
    Linux shell 编写(2)
    Linux shell 编写(1)
    团队冲刺(一)
    峦码团队任务表
    电梯演讲&界面展示说明
    第一次小组会议——NABCD讨论
    开发项目&团队介绍
    Linux中查看各文件夹大小命令:du -h --max-depth=1
    shell脚本[] [[]] -n -z 的含义解析
  • 原文地址:https://www.cnblogs.com/lipeil/p/4968477.html
Copyright © 2011-2022 走看看