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方法传入的模式。
  • 相关阅读:
    Climbing Stairs 爬楼梯问题,每次可以走1或2步,爬上n层楼梯总方法 (变相fibonacci)
    Search Insert Position 查找给定元素在数组中的位置,若没有则返回应该在的位置
    Remove Duplicates from Sorted List 去除链表中重复值节点
    Populating Next Right Pointers in Each Node 设置二叉树的next节点
    Binary Tree Inorder/Preorder Traversal 返回中序和前序/遍历二叉树的元素集合
    Same Tree 比较两个二叉树是否完全相同
    Linked List Cycle 判断一个链表是否存在回路(循环)
    Reverse Integer 旋转数字
    Maximum Depth of Binary Tree 二叉树的深度
    Single Number 数组中除了某个元素出现一次,其他都出现两次,找出这个元素
  • 原文地址:https://www.cnblogs.com/lipeil/p/4968477.html
Copyright © 2011-2022 走看看