[android]_[ViewGroup的基本使用]
来源:互联网 发布:vs2013怎么用c语言 编辑:程序博客网 时间:2024/04/23 19:37
1.自定义类继承ViewGroup,必须定义构造函数、定制控件位置函数(onLayout())、测量函数(onMeasure())。
protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int mViewGroupWidth = getMeasuredWidth(); //当前ViewGroup的总宽度 int mPainterPosX = left; //当前绘图光标横坐标位置 int mPainterPosY = top; //当前绘图光标纵坐标位置 int childCount = getChildCount(); for ( int i = 0; i < childCount; i++ ) { View childView = getChildAt(i); int width = childView.getMeasuredWidth(); int height = childView.getMeasuredHeight(); //如果剩余的空间不够,则移到下一行开始位置 if( mPainterPosX + width > mViewGroupWidth ) { mPainterPosX = left; mPainterPosY += height; } //执行ChildView的绘制 childView.layout(mPainterPosX,mPainterPosY,mPainterPosX+width, mPainterPosY+height); //记录当前已经绘制到的横坐标位置 mPainterPosX += width; } }xml中:
<com.titcktick.customview.CustomViewGroup xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="10dp" android:background="@android:color/black"/> <View android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="10dp" android:background="@android:color/black"/> <View android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="10dp" android:background="@android:color/black"/> <View android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="10dp" android:background="@android:color/black"/> </com.titcktick.customview.CustomViewGroup>
0 0
- [android]_[ViewGroup的基本使用]
- [android]_[ListView的基本使用]
- [Android]_[RxJava的基本使用]
- Android常用动画ViewGroup的使用
- Android ViewGroup系列控件的使用
- Android ViewGroup系列控件的使用
- Android 使用ViewGroup实现ViewPager的效果
- Android自定义ViewGroup的基本流程及用法演示
- Android ViewGroup使用小结
- Android ViewGroup使用小结
- [Android]_[注解库butterknife的基本使用]
- android里的ViewGroup
- android 的核心 ViewGroup
- ViewGroup的使用
- 自定义ViewGroup的使用
- 【android】ViewGroup中使用TabHost
- Android中ViewGroup等容器控件的使用
- android-使用ViewGroup实现左右滑动切换界面的效果
- MySQL索引使用的数据结构:B-Tree和B+Tree
- 架构 框架 设计模式的理解
- Obj 类的使用
- 管理Activity的生命周期
- MAC下设置mysql字符编码格式
- [android]_[ViewGroup的基本使用]
- 小猪的数据结构辅助教程——3.1 栈与队列中的顺序栈
- 怎么把自己的代码传到github上
- 2015,2016 占个坑
- 平面点集的三角剖分算法
- 利用C++11实现一个自动注册的工厂
- 学生成绩管理系统实验报告
- P2P金融
- SSH框架的整体认识