摘要:本文将带你了解Android应用开发之Android开发之自定义View2学习,希望本文对大家学Android有所帮助。
本文将带你了解Android应用开发之Android开发之自定义View2学习,希望本文对大家学Android有所帮助。
1.viewGroup绘制流程
总结:当继承ViewGroup的时候必须要重写onMeasure方法和onLayout方法,在onMeasure方法里面完成对孩子的测量,在onLayout方法里面完成对孩子的摆放. 当继承View的时候必须要重写onMeasure方法和onDraw方法 在onMeasure方法里面完成对当前view的测量,在onDraw完成绘制.
2.getMesasuredWidth和getwidth区别
getMesasuredWidth是对view测量后使用 getWidth 是对view排版后才可以使用
3.交叉布局
public class CrossLayout extends RelativeLayout {private boolean isLeft;public CrossLayout(Context context, AttributeSet attrs) {super(context, attrs);}//对孩子进行摆放@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {int top = 0;//1.找到所有的孩子for(int i=0;i<getChildCount();i++){//2.找到所有的孩子View child = getChildAt(i);//3.对孩子进行摆放int left = 0;//3.1 对isleft 判断 如果为true 就摆到左边 否则就摆到右边if (isLeft){//4.判断是奇数还是偶数 如果是奇数就摆到左边 如果是偶数就摆到右边if (i % 2 ==0){//对于控件是奇数 就摆到左边left = 0;}else{//对于控件来说是偶数 摆到右边 left = 容器宽度 - 孩子控件的宽度left = getMeasuredWidth() - child.getMeasuredWidth();}}else{//5.判断是奇数还是偶数 如果是奇数就摆到左边 如果是偶数就摆到右边if (i % 2 ==0){//对于控件是奇数 就摆到左边left = getMeasuredWidth() - child.getMeasuredWidth();}else{//对于控件来说是偶数 摆到右边 left = 容器宽度 - 孩子控件的宽度left = 0;}}int right = left+child.getMeasuredWidth();int bootom = top + child.getMeasuredHeight();child.layout(left,top,right,bootom);top += child.getMeasuredHeight();}}//用来实现布局切换public void startSwitchLayout() {isLeft =!isLeft;//请求重新排版requestLayout();}}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之Android频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号