自定义View由浅入深__ViewGroup(二)
来源:互联网 发布:软件项目管理 pdf 编辑:程序博客网 时间:2024/06/08 10:27
上一节,讲解了基本的控件的布局,这一节主要适配下父控件的Wap_content模式 ,这是一个基本模式,学会理解之后,以一敌万 . 滴水可以穿石 . 共勉
精髓部分贴出来,不贴布局文件了,
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int widthMode = MeasureSpec.getMode(widthMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);measureChildren(widthMeasureSpec, heightMeasureSpec);if (getChildCount() == 0) {// 如果没有子View.就设置父View宽高为0setMeasuredDimension(0, 0);} else if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {// ViewGroup,宽,高都是wrap_content,根据我们的需求,宽度是子控件的宽度,高度则是所有子控件的总和View childOne = getChildAt(0);int childWidth = childOne.getMeasuredWidth();int childHeight = childOne.getMeasuredHeight();setMeasuredDimension(childWidth, childHeight * getChildCount());} else if (widthMode == MeasureSpec.AT_MOST) {// ViewGroup的宽度为wrap_content,则高度不需要管,宽度还是自控件的宽度View childOne = getChildAt(0);int childWidth = childOne.getMeasuredWidth();setMeasuredDimension(childWidth, heightSize);} else if (heightMode == MeasureSpec.AT_MOST) {// ViewGroup的高度为wrap_content,则宽度不需要管,高度为子View的高度和View childOne = getChildAt(0);int childHeight = childOne.getMeasuredHeight();setMeasuredDimension(widthSize, childHeight * getChildCount());}}
细看一下,其实自定义View没有那么难 . 以前我也是看着onMesure这个方法晕, 其实看多了,写多了,就形成了一种潜移默化 .
全部代码如下
package com.cdl.demo;import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.View;import android.view.ViewGroup;public class MyViewGroup2 extends ViewGroup {public MyViewGroup2(Context context) {this(context, null);}public MyViewGroup2(Context context, AttributeSet attrs) {this(context, attrs, 0);}public MyViewGroup2(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int widthMode = MeasureSpec.getMode(widthMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);measureChildren(widthMeasureSpec, heightMeasureSpec);if (getChildCount() == 0) {// 如果没有子View.就设置父View宽高为0setMeasuredDimension(0, 0);} else if (widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST) {// ViewGroup,宽,高都是wrap_content,根据我们的需求,宽度是子控件的宽度,高度则是所有子控件的总和View childOne = getChildAt(0);int childWidth = childOne.getMeasuredWidth();int childHeight = childOne.getMeasuredHeight();setMeasuredDimension(childWidth, childHeight * getChildCount());} else if (widthMode == MeasureSpec.AT_MOST) {// ViewGroup的宽度为wrap_content,则高度不需要管,宽度还是自控件的宽度View childOne = getChildAt(0);int childWidth = childOne.getMeasuredWidth();setMeasuredDimension(childWidth, heightSize);} else if (heightMode == MeasureSpec.AT_MOST) {// ViewGroup的高度为wrap_content,则宽度不需要管,高度为子View的高度和View childOne = getChildAt(0);int childHeight = childOne.getMeasuredHeight();setMeasuredDimension(widthSize, childHeight * getChildCount());}}protected void onLayout(boolean changed, int l, int t, int r, int b) {int height = 0;int count = getChildCount();View child;for (int i = 0; i < count; i++) {child = getChildAt(i);child.layout(0, height, child.getMeasuredWidth(), height + child.getMeasuredHeight());height += child.getMeasuredHeight();}}}
0 0
- 自定义View由浅入深__ViewGroup(二)
- 自定义View由浅入深__ViewGroup(一)
- 自定义View由浅入深__ViewGroup(三)
- Android 自定义View (二)
- Android 自定义View (二)
- Android 自定义View (二)
- Android 自定义View (二)
- 自定义view(二)
- android 自定义view二
- 自定义view(二)
- 自定义View(二)
- 自定义view(二)
- 自定义View<二>
- 自定义View(二)
- Android自定义View(二)
- 自定义View(二)
- 自定义View二
- Android 自定义View 二
- [生存志] 第103节 范雎逐宣太后
- java中throw和throws区别
- PHP 使用 Redis
- excel tool: List objects / file and folders
- Windows下配置Git服务器和客户端
- 自定义View由浅入深__ViewGroup(二)
- Ubuntu使用docker安装redmine
- 使用Swing制作java验证码
- 设计模式(四)——建造者模式
- Logistic回归与梯度下降法
- QPushButton
- Ubuntu新手-谈第一次在Ubuntu升级VMware Tolls
- 分治
- 欢迎使用CSDN-markdown编辑器