Android绘图机制(三) ViewGroup类的延伸

继续上一篇,先将文档中的内容补充完整,后面部分解释上一篇中的一些方法。

如果需要项实例中一样实现XML布局属性,则需要如下方式在attrs.xml文件中申明自定义属性

res/values/attrs.xml:
<declare-styleable name="CustomLayoutLP">
    <attr name="android:layout_gravity" />
    <attr name="layout_position">
        <enum name="middle" value="0" />
        <enum name="left" value="1" />
        <enum name="right" value="2" />
    </attr>
</declare-styleable>

在xml布局文件中使用上述自定义布局管理器

<com.example.android.apis.view.CustomLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <!-- 将第一个视图放到左边-->
    <TextView
            android:background="@drawable/filled_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_position="left"
            android:layout_gravity="fill_vertical|center_horizontal"
            android:text="l1"/>

    <!-- 将第二个视图叠加到左边-->
    <TextView
            android:background="@drawable/filled_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_position="left"
            android:layout_gravity="fill_vertical|center_horizontal"
            android:text="l2"/>

    <!-- 将此视图放到右边-->
    <TextView
            android:background="@drawable/filled_box"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_position="right"
            android:layout_gravity="fill_vertical|center_horizontal"
            android:text="r1"/>

    <!--  默认情况下,视图放到布局中间,使用重心垂直填充 -->
    <TextView
            android:background="@drawable/green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="fill_vertical|center_horizontal"
            android:text="fill-vert"/>

    <!-- <span style="font-family: Arial, Helvetica, sans-serif;"> 默认情况下,视图放到布局中间,使用重心水平填充 </span><span style="font-family: Arial, Helvetica, sans-serif;">--></span>
    <TextView
            android:background="@drawable/green"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|fill_horizontal"
            android:text="fill-horiz"/>

    <!--  <span style="font-family: Arial, Helvetica, sans-serif;">默认情况下,视图放到布局中间,使用重心左上角填充  </span><span style="font-family: Arial, Helvetica, sans-serif;">--></span>
    <TextView
            android:background="@drawable/blue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|left"
            android:text="top-left"/>

    <!--  <span style="font-family: Arial, Helvetica, sans-serif;">默认情况下,视图放到布局中间,使用重心重心填充 </span><span style="font-family: Arial, Helvetica, sans-serif;">--></span>
    <TextView
            android:background="@drawable/blue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="center"/>

    <!--  <span style="font-family: Arial, Helvetica, sans-serif;">默认情况下,视图放到布局中间,使用重心右下角填充  </span>-->
    <TextView
            android:background="@drawable/blue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:text="bottom-right"/>

</com.example.android.apis.view.CustomLayout>

上篇文章中部分的方法解释:
protected void measureChildWithMargins (View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)
方法解释:让视图其中一个子视图测量它自己,并将该视图的测量规格MeasureSpec 要求和其padding,margin值计算在内,子视图必须有一个MarginLayoutParams 值,并可通过getChildMeasureSpec()方法获取该对象。
参数解释:

参数描述
child将要测量自己的子视图
parentWidthMeasureSpec该子视图的宽度要求Pipe
widthUsed父视图中可能已经被其他子视图使用的横向空间
parentHeightMeasureSpec该子视图已经使用的高度要求
heightUsed父视图中可能已经被其他子视图使用的竖直空间

方法解释:将通过public static int combineMeasuredStates (int curState, int newState) 方法返回的两个视图状态值合并。
参数解释:

参数描述
curState视图或合并后的视图返回的当前状态
newState组合的新视图状态
   返回值:表示两个状态组合后的新的状态值,整数。

public static int resolveSizeAndState (int size, int measureSpec, int childMeasuredState)
方法解释:依据测量规格MeasureSpec暴露出的约束值协调期望尺寸和视图状态的公共方法),如果约束中没有设置其他不同的值,则取期望值。返回值是一个组合整数。.如果结果值比视图的期望值小,则会将这个之分解后设置到MEASURED_SIZE_MASK 和MEASURED_STATE_TOO_SMALL 的二进制值中。

    参数解释:
参数描述
size视图的期望尺寸值
measureSpec父视图暴露的约束值
    返回值:尺寸信息的二进制表示。总共四个:MEASURED_SIZE_MASK (提供视图实际尺寸的方法getMeasuredWidthAndState()和 getMeasuredWidthAndState()返回值得机器码值,常量值 16777215 (0x00ffffff) )
    MEASURED_STATE_TOO_SMALL ( 提供视图实际尺寸的方法getMeasuredWidthAndState()和 getMeasuredWidthAndState()返回值的机器码值比视图的期望尺寸值小,常量值16777216 (0x01000000) ) 

    MEASURED_STATE_MASK(提供视图附加状态值的方法getMeasuredWidthAndState()和 getMeasuredWidthAndState()返回值的机器码值比,常量值-16777216 (0xff000000) )

    MEASURED_HEIGHT_STATE_SHIFT(MEASURED_STATE_MASK 字节偏移,使组合宽度和高度到单个整数的方法获取视图的高度字节值,如方法getMeasuredState() 和 resolveSizeAndState(int, int, int), 常量值16 (0x00000010)) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值