android 自定义View(三) 自定义ViewGroup之可添加的view
来源:互联网 发布:c语言在线编程软件 编辑:程序博客网 时间:2024/05/22 05:13
在这章我,我们将通过自定义的一个ViewGroup来实现一个类似于今日头条中编辑分类添加的效果。
首先,需要先继承一个ViewGroup。
public class MyViewGroup extends ViewGroup{ public MyViewGroup(Context context,AttributeSet attrs) { super(context); // TODO Auto-generated constructor stub addView(CreatView()); } }
因为在这个group中可以添加新的子view,所有需要对view的大小左处理
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec);// int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);// int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);// int modeWidth = MeasureSpec.getMode(widthMeasureSpec);// int modeHeight = MeasureSpec.getMode(heightMeasureSpec); int childCount = getChildCount(); Log.d("HK", "childCount = "+childCount); if(childCount == 0) { setMeasuredDimension(0, 0); Log.d("HK", "setMeasuredDimension(0, 0)"); } else { Log.d("HK", "setMeasuredDimension(chirldWidth, chirldHight)"); for(int i=0;i<childCount;i++) { TextView tvChild = (TextView) getChildAt(i); measureChild(tvChild, widthMeasureSpec, heightMeasureSpec); int childWidth = tvChild.getMeasuredWidth(); int childHight = tvChild.getMeasuredHeight(); int row = i-1/4; Log.d("HK", "childWitch = "+childWidth); setMeasuredDimension(childWidth*4, (childHight)*(row+1)); Log.d("HK", "setMeasuredDimension w h = "+childWidth*4+" "+childHight*(row+1)); } } }
这面通过多次Measure子view的高和宽来设置viewGroup的高宽,此处保证每行4个,超过4个则添加一行。在设置好viewGroup后,接下来就需要将子View安放在里面。
protected void onLayout(boolean changed, int l, int t, int r, int b) { // TODO Auto-generated method stub int childCount = getChildCount(); int left = 0; //int row = childCount/4; int mod = childCount%4; int row = 0; TextView childView; for(int i=0;i<childCount;i++) { childView = (TextView) getChildAt(i); if(i==childCount-1) { //给最后一个子view添加点击事件,通过点击可以新建一个子view childView.setText("点击添加"); childView.setBackgroundColor(Color.BLUE); childView.setTextColor(Color.BLACK); childView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Log.d("HK", "onLayout Click"); addView(CreatView()); //requestLayout(); invalidate(); } }); } else { childView.setText("新加入的"); childView.setTextColor(Color.BLACK); childView.setBackgroundColor(Color.GREEN); } int width = childView.getMeasuredWidth(); Log.d("HK", "onLayout = "+width+" "+childView.getHeight()); row = i/4; if((i+1)%4==1&&row!=0) { left = 0; Log.d("HK", "onLayout change line"); } childView.layout(left, childView.getMeasuredHeight()*row, left+width, childView.getMeasuredHeight()*(row+1)); Log.d("HK", "childView.layout left top right buttom= "+left+" "+0+" "+(left+width)+" "+childView.getMeasuredHeight()); left += width; Log.d("HK", "onLayout row = "+row +" "+i%4+" "+i); } } //新建子view的方法 protected TextView CreatView() { TextView tv = new TextView(MyContext.getContext()); tv.setHeight(80); tv.setWidth(220); return tv; }
详细代码请见github
GITHUB:https://github.com/everyhappy/myViews
0 0
- android 自定义View(三) 自定义ViewGroup之可添加的view
- android自定义View(viewGroup)
- android自定义View、ViewGroup
- Android 自定义view(三) 继承ViewGroup
- Android自定义ViewGroup添加组合控件的子view
- android 自定义 view 之ViewGroup(四)
- <Android 进阶(三)> 自定义View之支持Gravity的ViewGroup
- Android自定义View或ViewGroup的流程
- android 自定义View开发实战(三) 自定义ViewGroup--FourLayout
- Android 自定义View 和 ViewGroup
- Android 自定义View和ViewGroup
- Android自定义View和ViewGroup
- Android开发之自定义View专题(四):自定义ViewGroup
- 自定义ViewGroup,子View可对换位置
- android View的详解 之 自定义view (三)
- 自定义view,继承ViewGroup实现可换行的布局容器
- android自定义View&自定义ViewGroup(上)
- android自定义View&自定义ViewGroup(下)
- 解决字符集乱码方法集合
- Android开发——布局性能优化的一些技巧(一)
- Python模块包中__init__.py文件的作用
- pip更换软件镜像源
- 牛客网 - 剑指Offer - 考点:链表 题目:从尾到头打印链表
- android 自定义View(三) 自定义ViewGroup之可添加的view
- 第八周训练8污损的单据
- mycat配置mysql简单的读写分离
- Java设计模式学习03——建造者模式
- SSM实现最简单的登录验证
- iOS 修改UITextField 的placeholder字体大小和颜色
- Unity Shader基础(1)
- JAVA学习笔记001
- Codeforces Round #377 (Div. 2) C 模拟