常用的自定义View例子一(流布式布局)

childStartLayoutY += childMaxHeight + verticalSpacing;

childStartLayoutX = paddingLeft;

widthUsed = paddingLeft + paddingRight;

left = childStartLayoutX + leftMargin;

top = childStartLayoutY + topMargin;

right = left + childMeasuredWidth;

bottom = top + childMeasuredHeight;

widthUsed += childNeededWidth;

childStartLayoutX += childNeededWidth;

childMaxHeight = childNeedHeight;

}

child.layout(left, top, right, bottom);

}

}

}

private Rect getMarginRect(View child) {

LayoutParams layoutParams = child.getLayoutParams();

int leftMargin = 0;

int rightMargin = 0;

int topMargin = 0;

int bottomMargin = 0;

if (layoutParams instanceof MarginLayoutParams) {

MarginLayoutParams marginLayoutParams = (MarginLayoutParams) layoutParams;

leftMargin = marginLayoutParams.leftMargin;

rightMargin = marginLayoutParams.rightMargin;

topMargin = marginLayoutParams.topMargin;

bottomMargin = marginLayoutParams.bottomMargin;

}

return new Rect(leftMargin, topMargin, rightMargin, bottomMargin);

}

}

2)思路解析

  1. 首先我们重写onMeasure方法,在OnMeasure方法里面我们调用measureChild()这个方法去获取每个孩子的宽度和高度,每次增加一个孩子我们执行 widthUsed += childUsedWidth;

  2. 添加完一个孩子以后我们判断widthUsed是够超出控件本身的最大宽度widthSpecSize,

若没有超过执行

widthUsed += childUsedWidth;

if (childUsedHeight > childMaxHeightOfThisLine) {

childMaxHeightOfThisLine = childUsedHeight;

}

超过控件的宽度执行

heightUsed += childMaxHeightOfThisLine + verticalSpacing;

widthUsed = paddingLeft + paddingRight + childUsedWidth;

childMaxHeightOfThisLine = childUsedHeight;

最后调用 setMeasuredDimension(widthSpecSize, heightUsed);这个方法去设置它的大小

3.在OnLayout方法里面,所做的工作就是去摆放每一个孩子的位置 ,判断需不需要换行,不需要更改left值,需要换行,更改top值

3)注意事项

讲解之前,我们先来了解一下一个基本知识

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传


从这张图片里面我们可以得出这样结论

  1. Width=控件真正的宽度(realWidth)+PaddingLeft+PaddingRight

  2. margin是子控件相对于父控件的距离

注意事项

  1. 为了支持控件本身的padding属性,我们做了处理,主要代码如下

int widthUsed = paddingLeft + paddingRight;

int heightUsed = paddingTop + paddingBottom;


if (widthUsed + childUsedWidth < widthSpecSize) {

widthUsed += childUsedWidth;

if (childUsedHeight > childMaxHeightOfThisLine) {

childMaxHeightOfThisLine = childUsedHeight;

}

}

  1. 为了支持子控件的margin属性,我们同样也做了处理

Rect marginRect = getMarginRect(child);

int leftMargin=marginRect.left;

int rightMargin=marginRect.right;

int topMargin=marginRect.top;

int bottomMargin=marginRect.bottom;

childUsedWidth += leftMargin + rightMargin;

childUsedHeight += topMargin + bottomMargin;

即我们在计算孩子所占用的宽度和高度的时候加上margin属性的高度,接着在计算需要孩子总共用的宽高度的时候加上每个孩子的margin属性的宽高度,这样自然就支持了孩子的margin属性了

4.缺陷

如下图所见,在控件宽度参差不齐的情况下,控件换行会留下一些剩余的宽度,作为想写出鲁棒性的代码的我们会觉得别扭,于是我们相处了解决办法。

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

解决方法见下面

图二源码解析


外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

废话不多说,先看源码

/**

  • 博客地址:http://blog.csdn.net/gdutxiaoxu

  • @author xujun

  • @time 2016/6/26 22:54.

*/

public class PrefectFlowLayout extends ViewGroup {

public PrefectFlowLayout(Context context) {

super(context);

}

public PrefectFlowLayout(Context context, AttributeSet attrs) {

super(context, attrs);

}

public PrefectFlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

}

//父容器宽度

private int parentWidthSize;

//水平间距

private int horizontalSpacing = 16;

//垂直间距

private int verticalSpacing = 16;

//当前行

private Line currentLine;

//所有行的集合

private List mLines = new ArrayList<>();

//当前行已使用宽度

private int userWidth = 0;

/**

  • 行对象

*/

private class Line {

//一行里面所添加的子View集合

private List children;

//当前行高度

private int height;

//当前行已使用宽度

private int lineWidth = 0;

public Line() {

children = new ArrayList<>();

}

/**

  • 添加一个子控件

  • @param child

*/

private void addChild(View child) {

children.add(child);

if (child.getMeasuredHeight() > height) {

//当前行高度以子控件最大高度为准

height = child.getMeasuredHeight();

}

//将每个子控件宽度进行累加,记录使用的宽度

lineWidth += child.getMeasuredWidth();

}

/**

  • 获取行的高度

  • @return

*/

public int getHeight() {

return height;

}

/**

  • 获取子控件的数量

  • @return

*/

public int getChildCount() {

return children.size();

}

/**

  • 放置每一行里面的子控件的位置

  • @param l 距离最左边的距离

  • @param t 距离最顶端的距离

*/

public void onLayout(int l, int t) {

//当前行使用的宽度,等于每个子控件宽度之和+子控件之间的水平距离

lineWidth += horizontalSpacing * (children.size() - 1);

int surplusChild = 0;

int surplus = parentWidthSize - lineWidth;//剩余宽度

if (surplus > 0) {

//如果有剩余宽度,则将剩余宽度平分给每一个子控件

surplusChild = (int) (surplus / children.size()+0.5);

}

for (int i = 0; i < children.size(); i++) {

View child = children.get(i);

child.getLayoutParams().width=child.getMeasuredWidth()+surplusChild;

if (surplusChild>0){

//如果长度改变了后,需要重新测量,否则布局中的属性大小还会是原来的大小

child.measure(MeasureSpec.makeMeasureSpec(

child.getMeasuredWidth()+surplusChild,MeasureSpec.EXACTLY)

,MeasureSpec.makeMeasureSpec(height,MeasureSpec.EXACTLY));

}

child.layout(l, t, l + child.getMeasuredWidth(), t + child.getMeasuredHeight());

l += child.getMeasuredWidth() + horizontalSpacing;

}

}

}

// getMeasuredWidth() 控件实际的大小

// getWidth() 控件显示的大小

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

//将之前测量的数据进行清空,以防复用时影响下次测量

mLines.clear();

currentLine = null;

userWidth = 0;

//获取父容器的宽度和模式

int widthMode = MeasureSpec.getMode(widthMeasureSpec);

parentWidthSize = MeasureSpec.getSize(widthMeasureSpec)

  • getPaddingLeft() - getPaddingRight();

//获取父容器的高度和模式

int heigthMode = MeasureSpec.getMode(heightMeasureSpec);

int heightSize = MeasureSpec.getSize(heightMeasureSpec)

  • getPaddingTop() - getPaddingBottom();

int childWidthMode, childHeightMode;

//为了测量每个子控件,需要指定每个子控件的测量规则

//子控件设置为WRAP_CONTENT,具体测量规则详见,ViewGroup的getChildMeasureSpec()方法

if (widthMode == MeasureSpec.EXACTLY) {

childWidthMode = MeasureSpec.AT_MOST;

} else {

childWidthMode = widthMode;

}

if (heigthMode == MeasureSpec.EXACTLY) {

childHeightMode = MeasureSpec.AT_MOST;

} else {

childHeightMode = heigthMode;

}

//获取到子控件高和宽的测量规则

int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(parentWidthSize, childWidthMode);

int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, childHeightMode);

currentLine = new Line();//创建第一行

for (int i = 0; i < getChildCount(); i++) {

View child = getChildAt(i);

//测量每一个孩子

child.measure(childWidthMeasureSpec, childHeightMeasureSpec);

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

最后

其实Android开发的知识点就那么多,面试问来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。

这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司21年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

由于篇幅有限,这里以图片的形式给大家展示一小部分。

详细整理在GitHub可以见;

Android架构视频+BAT面试专题PDF+学习笔记​

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

来问去还是那么点东西。所以面试没有其他的诀窍,只看你对这些知识点准备的充分程度。so,出去面试时先看看自己复习到了哪个阶段就好。

虽然 Android 没有前几年火热了,已经过去了会四大组件就能找到高薪职位的时代了。这只能说明 Android 中级以下的岗位饱和了,现在高级工程师还是比较缺少的,很多高级职位给的薪资真的特别高(钱多也不一定能找到合适的),所以努力让自己成为高级工程师才是最重要的。

这里附上上述的面试题相关的几十套字节跳动,京东,小米,腾讯、头条、阿里、美团等公司21年的面试题。把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节。

由于篇幅有限,这里以图片的形式给大家展示一小部分。

[外链图片转存中…(img-la4cDtB8-1710698746025)]

详细整理在GitHub可以见;

Android架构视频+BAT面试专题PDF+学习笔记​

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值