Android 自定义UI 实战 02 流式布局,2024年最新嵌入式工程师面经

<Button

android:layout_width=“wrap_content”

android:layout_height=“100dp”

android:text=“发电房” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“小麻小儿郎呀” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“75dp”

android:text=“Hello hi …” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“你是谁呀” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“bottom”

android:text=“人在他在,塔亡人亡” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“生活不止眼前的苟且,还有诗和远方” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“发电房” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“80dp”

android:text=“小麻小儿郎呀” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“Hello hi …” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“你是谁呀” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_gravity=“bottom”

android:text=“人在他在,塔亡人亡” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“300dp”

android:text=“生活不止眼前的苟且,还有诗和远方” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“发电房” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“小麻小儿郎呀” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“Hello hi …” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“你是谁呀” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“65dp”

android:layout_gravity=“bottom”

android:text=“人在他在,塔亡人亡” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“生活不止眼前的苟且,还有诗和远方1” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“250dp”

android:text=“发电房” />

<Button

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:text=“这是结束” />

</com.traveleasy.leaningui02.FlowLayout>

2、初始化存储


初始化三个数组,用于存储行数据,所有的行数据,行高

// 每一行的view

private List mLineView;

// 所有的行,一行一行的存储

private List<List> mViews;

// 每一行的高度

private List mHeights;

private void init() {

mLineView = new ArrayList<>();

mViews = new ArrayList<>();

mHeights = new ArrayList<>();

}

3、测量


重新 onMeasure() 方法,测量view 的属性

源码的解释,都在代码中,如下:

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// 获取限制的值

int widthMode = MeasureSpec.getMode(widthMeasureSpec);

int widthSize = MeasureSpec.getSize(widthMeasureSpec);

int heightNode = MeasureSpec.getMode(heightMeasureSpec);

int heightSize = MeasureSpec.getSize(heightMeasureSpec);

// 记录当前行的宽度和高度

int lineWidth = 0; //宽度是当前行子view 宽度的和

int lineHeight = 0; //高度是当前行所有子view 中高度的最大值

// 整个流式布局的宽度和高度

int flowLayoutWidth = 0; // 所有行中宽度的最大值

int flowLayoutHeight = 0; // 所有行的高度的累加

init();

// 获取到当前的所有child 数量

int childCount = this.getChildCount();

// 先测量子View,再根据子View 尺寸, 计算自己的

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

View child = this.getChildAt(i);

measureChild(child, widthMeasureSpec, heightMeasureSpec);

// 获取到当前子View 的测量高度 和 宽度

int childWidth = child.getMeasuredWidth();

int childHeight = child.getMeasuredHeight();

// 已经放入的子 view 的宽度 + 准备放入的子 view 的宽度 大于 总宽度,就换行

if (lineWidth + childWidth > widthSize) {

mViews.add(mLineViews);

// 创建 新的一行

mLineViews = new ArrayList<>();

// 所有行中,最宽的一行,作为流式布局的宽

flowLayoutWidth = Math.max(flowLayoutWidth, lineWidth);

// 流式布局的高度,为所有行的高度相加

flowLayoutHeight += lineHeight;

mHeights.add(lineHeight);

lineHeight = 0;

lineWidth = 0;

}

// 当前行添加子view

mLineViews.add(child);

// 已有行宽,添加当前 子 view 的宽

lineWidth += childWidth;

// 获取行中最高的子 View

lineHeight = Math.max(lineHeight, childHeight);

}

// 保存尺寸给后面使用

setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : flowLayoutWidth,

heightNode == MeasureSpec.EXACTLY ? heightSize : flowLayoutHeight);

}

4、布局处理


@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

int currX = 0;

int currY = 0;

int lineCount = mViews.size();

// 处理每一行

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

List lineViews = mViews.get(i);

int lineHeight = mHeights.get(i);

int size = lineViews.size();

// 处理每一行中的view

for (int j = 0; j < size; j++) {

View child = lineViews.get(j);

// 子view 的左上右下

int left = currX;

int top = currY;

int right = left + child.getMeasuredWidth();

int bottom = top + child.getMeasuredHeight();

// 布局子view

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

currX += child.getMeasuredWidth();

}

currY += lineHeight;

currX = 0;

}

}

5、初步效果图


在这里插入图片描述

6、添加 padding margin 约束


到这就完了吗?当然没有,如图所示,没有约束啊,设置的样式无效。我们想要的效果如下图:

在这里插入图片描述

修改完之后的代码如下:

public class FlowLayout extends ViewGroup {

// 每一行的view

private List mLineViews;

// 所有的行,一行一行的存储

private List<List> mViews;

// 每一行的高度

private List mHeights;

public FlowLayout(Context context) {

this(context, null);

}

public FlowLayout(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

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

super(context, attrs, defStyleAttr);

init();

}

private void init() {

mLineViews = new ArrayList<>();

mViews = new ArrayList<>();

mHeights = new ArrayList<>();

}

@Override

public LayoutParams generateLayoutParams(AttributeSet attrs) {

return new MarginLayoutParams(getContext(), attrs);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// 获取限制的值

int widthMode = MeasureSpec.getMode(widthMeasureSpec);

int widthSize = MeasureSpec.getSize(widthMeasureSpec);

int heightNode = MeasureSpec.getMode(heightMeasureSpec);

int heightSize = MeasureSpec.getSize(heightMeasureSpec);

int paddingLeftAndRight = getPaddingLeft() + getPaddingRight();

int paddingTopAndBottom = getPaddingTop() + getPaddingTop();

// 解决子 view match_parent 无效的问题

heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);

// 记录当前行的宽度和高度

int lineWidth = 0; //宽度是当前行子view 宽度的和

int lineHeight = 0; //高度是当前行所有子view 中高度的最大值

// 整个流式布局的宽度和高度

int flowLayoutWidth = 0; // 所有行中宽度的最大值

int flowLayoutHeight = 0; // 所有行的高度的累加

init();

// 获取到当前的所有child 数量

int childCount = this.getChildCount();

// 先测量子View,再根据子View 尺寸, 计算自己的

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

View child = this.getChildAt(i);

// measureChild(child, widthMeasureSpec, heightMeasureSpec);

measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);

MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();

int childMarginLeftAndRight = lp.leftMargin + lp.rightMargin;

int childMarginTopAndBottom = lp.topMargin + lp.bottomMargin;

// 获取到当前子View 的测量高度 和 宽度

int childWidth = child.getMeasuredWidth();

int childHeight = child.getMeasuredHeight();

// 已经放入的子 view 的宽度 + 准备放入的子 view 的宽度 大于 总宽度,就换行

if (lineWidth + childWidth + childMarginLeftAndRight + paddingLeftAndRight > widthSize) {

mViews.add(mLineViews);

// 创建 新的一行

mLineViews = new ArrayList<>();

// 所有行中,最宽的一行,作为流式布局的宽

flowLayoutWidth = Math.max(flowLayoutWidth, lineWidth);

// 流式布局的高度,为所有行的高度相加

flowLayoutHeight += lineHeight;

mHeights.add(lineHeight);

lineHeight = 0;

lineWidth = 0;

}

// 当前行添加子view

mLineViews.add(child);

// 已有行宽,添加当前 子 view 的宽

lineWidth += childWidth + childMarginLeftAndRight;

// 获取行中最高的子 View

lineHeight = Math.max(lineHeight, childHeight + childMarginTopAndBottom);

// 处理最后一行的显示

if (i == childCount - 1){

flowLayoutHeight += lineHeight;

flowLayoutWidth = Math.max(flowLayoutWidth, lineWidth);

mHeights.add(lineHeight);

mViews.add(mLineViews);

}

}

flowLayoutWidth += paddingLeftAndRight;

flowLayoutHeight += paddingTopAndBottom;

// 保存尺寸给后面使用

setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? widthSize : flowLayoutWidth,

heightNode == MeasureSpec.EXACTLY ? heightSize : flowLayoutHeight);

}

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

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

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

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

尾声

开发是需要一定的基础的,我是08年开始进入Android这行的,在这期间经历了Android的鼎盛时期,和所谓的Android”凉了“。中间当然也有着,不可说的心酸,看着身边朋友,同事一个个转前端,换行业,其实当时我的心也有过犹豫,但是我还是坚持下来了,这次的疫情就是一个好的机会,大浪淘沙,优胜劣汰。再等等,说不定下一个黄金浪潮就被你等到了。

  • 330页 PDF Android核心笔记

  • 几十套阿里 、字节跳动、腾讯、华为、美团等公司2020年的面试题

  • PDF和思维脑图,包含知识脉络 + 诸多细节

  • Android进阶系统学习视频

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img
帮助,可以添加V获取:vip204888 (备注Android)**
[外链图片转存中…(img-2ekY5CZs-1712608455699)]

尾声

开发是需要一定的基础的,我是08年开始进入Android这行的,在这期间经历了Android的鼎盛时期,和所谓的Android”凉了“。中间当然也有着,不可说的心酸,看着身边朋友,同事一个个转前端,换行业,其实当时我的心也有过犹豫,但是我还是坚持下来了,这次的疫情就是一个好的机会,大浪淘沙,优胜劣汰。再等等,说不定下一个黄金浪潮就被你等到了。

  • 330页 PDF Android核心笔记

[外链图片转存中…(img-xSQ3vq7n-1712608455699)]

  • 几十套阿里 、字节跳动、腾讯、华为、美团等公司2020年的面试题

[外链图片转存中…(img-188r3N2K-1712608455700)]

[外链图片转存中…(img-G5Nt7tkV-1712608455700)]

  • PDF和思维脑图,包含知识脉络 + 诸多细节

[外链图片转存中…(img-eA9GHcMT-1712608455701)]

  • Android进阶系统学习视频

[外链图片转存中…(img-mwja6MQC-1712608455701)]

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-nNRLDwAc-1712608455701)]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值