自定义控件ViewGroup
2018-02-13 18:04
85 查看
对于ViewGroup来说就是一个里面放了许多的View的东西。所以想要自定义一个ViewGroup比自定义View要稍微复杂一点。
1.重写onMeasure()方法
2.重写onLauyout()方法
3.处理触摸事件
onMeasure() 跟自定义View一样,需要测量好并给其设置测量好的宽高,但ViewGroup的跟View不太一样的地方在于,View需要测量的是自己,而ViewGroup则是测量自己里面的子View,并根据子View的测量来设置自己的宽高。
left:ViewGroup的left到子View的left的距离
top:ViewGroup的top到子View的top的距离
right:ViewGroup的left到子View的right的距离
bottom:ViewGroup的top到子View的bottom的距离。
1.重写onMeasure()方法
2.重写onLauyout()方法
3.处理触摸事件
onMeasure() 跟自定义View一样,需要测量好并给其设置测量好的宽高,但ViewGroup的跟View不太一样的地方在于,View需要测量的是自己,而ViewGroup则是测量自己里面的子View,并根据子View的测量来设置自己的宽高。
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); height = 0; width = MeasureSpec.getSize(widthMeasureSpec); for (int i = 0; i < getChildCount(); i++) { //遍历子View,并测量 View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); height += child.getMeasuredHeight(); } setMeasuredDimension(width, height); }
left:ViewGroup的left到子View的left的距离
top:ViewGroup的top到子View的top的距离
right:ViewGroup的left到子View的right的距离
bottom:ViewGroup的top到子View的bottom的距离。
相关文章推荐
- Android自定义ViewGroup实现可滚动的横向布局(2)
- Android自定义viewGroup实现点击动画效果
- Android自定义viewgroup快速滑动(4)
- Android动画效果之自定义ViewGroup添加布局动画(五)
- Android App开发中自定义View和ViewGroup的实例教程
- 从源码解析Android中View的容器ViewGroup
- Android UI设计系列之自定义ViewGroup打造通用的关闭键盘小控件ImeObserverLayout(9)
- Android应用开发中自定义ViewGroup视图容器的教程
- Android自定义ViewGroup实现受边界限制的滚动操作(3)
- Android继承ViewGroup实现Scroll滑动效果的方法示例
- Android自定义ViewGroup之第一次接触ViewGroup
- Android自定义ViewGroup实现弹性滑动效果
- Android 重写ViewGroup 分析onMeasure()和onLayout()方法
- Android中使用自定义ViewGroup的总结
- Android自定义ViewGroup打造各种风格的SlidingMenu
- Android自定义ViewGroup实现标签流容器FlowLayout
- Android自定义ViewGroup实现带箭头的圆角矩形菜单
- Android事件分发机制(上) ViewGroup的事件分发
- Android自定义ViewGroup实现标签浮动效果
- Android视图控件架构分析之View、ViewGroup