Android基础到进阶UI祖父级 ViewGroup介绍+实用

if (attrArray != null) {

childHorizontalSpace = attrArray.getDimensionPixelSize(R.styleable.CustomLayout_horizontalSpace, 12);

childVerticalSpace = attrArray.getDimensionPixelSize(R.styleable.CustomLayout_verticalSpace, 12);

MLog.e(getClass().getName(),“HorizontalSpace:”+childHorizontalSpace+“|VerticalSpace:”+childVerticalSpace);

attrArray.recycle();

}

//此视图是否自行绘制

setWillNotDraw(false);

}

/**

  • 负责设置子控件的测量模式和大小 根据所有子控件设置自己的宽和高

*/

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

MLog.e(getClass().getName(),“onMeasure”);

// 获得它的父容器为它设置的测量模式和大小

int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);

int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

int modeWidth = MeasureSpec.getMode(widthMeasureSpec);

int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

// 如果是warp_content情况下,记录宽和高

int width = 0;

int height = 0;

//记录每一行的宽度,width不断取最大宽度

int lineWidth = 0;

//每一行的高度,累加至height

int lineHeight = 0;

int count = getChildCount();

int left = getPaddingLeft();

int top = getPaddingTop();

// 遍历每个子元素

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

View child = getChildAt(i);

if (child.getVisibility() == GONE)

continue;

// 测量每一个child的宽和高

measureChild(child, widthMeasureSpec, heightMeasureSpec);

// 得到child的lp

ViewGroup.LayoutParams lp = child.getLayoutParams();

// 当前子空间实际占据的宽度

int childWidth = child.getMeasuredWidth() + childHorizontalSpace;

// 当前子空间实际占据的高度

int childHeight = child.getMeasuredHeight() + childVerticalSpace;

if (lp != null && lp instanceof MarginLayoutParams) {

MarginLayoutParams params = (MarginLayoutParams) lp;

childWidth += params.leftMargin + params.rightMargin;

childHeight += params.topMargin + params.bottomMargin;

}

//如果加入当前child,则超出最大宽度,则的到目前最大宽度给width,类加height 然后开启新行

if (lineWidth + childWidth > sizeWidth - getPaddingLeft() - getPaddingRight()) {

width = Math.max(lineWidth, childWidth);// 取最大的

lineWidth = childWidth; // 重新开启新行,开始记录

// 叠加当前高度,

height += lineHeight;

// 开启记录下一行的高度

lineHeight = childHeight;

child.setTag(new Location(left, top + height, childWidth + left - childHorizontalSpace, height + child.getMeasuredHeight() + top));

} else {

// 否则累加值lineWidth,lineHeight取最大高度

child.setTag(new Location(lineWidth + left, top + height, lineWidth + childWidth - childHorizontalSpace + left, height + child.getMeasuredHeight() + top));

lineWidth += childWidth;

lineHeight = Math.max(lineHeight, childHeight);

}

}

width = Math.max(width, lineWidth) + getPaddingLeft() + getPaddingRight();

height += lineHeight;

sizeHeight += getPaddingTop() + getPaddingBottom();

height += getPaddingTop() + getPaddingBottom();

setMeasuredDimension((modeWidth == MeasureSpec.EXACTLY) ? sizeWidth : width, (modeHeight == MeasureSpec.EXACTLY) ? sizeHeight : height);

}

/**

  • 记录子控件的坐标

*/

public class Location {

public Location(int left, int top, int right, int bottom) {

this.left = left;

this.top = top;

this.right = right;

this.bottom = bottom;

}

public int left;

public int top;

public int right;

public int bottom;

}

//计算当前View以及子View的位置

@Override

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

MLog.e(getClass().getName(),“onLayout”);

//获取子View个数

int count = getChildCount();

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

//获取子View

View child = getChildAt(i);

//判断是否显示

if (child.getVisibility() == GONE)

continue;

//获取子View的坐标

Location location = (Location) child.getTag();

//设置子View位置

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

}

}

@Override

protected void onSizeChanged(int w, int h, int oldw, int oldh) {

super.onSizeChanged(w, h, oldw, oldh);

MLog.e(getClass().getName(),“onSizeChanged”);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

MLog.e(getClass().getName(),“onDraw”);

}

}

2.使用自定义CustomLayout

<?xml version="1.0" encoding="utf-8"?>

<com.scc.demo.view.CustomLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:custom=“http://schemas.android.com/apk/res-auto”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:layout_margin=“@dimen/dimen_20”

custom:horizontalSpace=“10dp”

custom:verticalSpace=“20dp”>

<TextView

style=“@style/TvStyle”

android:text=“破阵子·为陈同甫赋壮词以寄” />

<TextView

style=“@style/TvStyle”

android:text=“宋·辛弃疾” />

<TextView

style=“@style/TvStyle”

android:text=“醉里挑灯看剑” />

<TextView

style=“@style/TvStyle”

android:text=“梦回吹角连营” />

<TextView

style=“@style/TvStyle”

android:text=“八百里分麾下炙” />

<TextView

style=“@style/TvStyle”

android:text=“五十弦翻塞外声” />

<TextView

style=“@style/TvStyle”

android:text=“沙场秋点兵” />

<TextView

style=“@style/TvStyle”

android:text=“马作的卢飞快” />

<TextView

style=“@style/TvStyle”

android:text=“弓如霹雳弦惊(增加点长度)” />

<TextView

style=“@style/TvStyle”

android:text=“了却君王天下事” />

<TextView

style=“@style/TvStyle”

android:text=“赢得生前身后名” />

<TextView

style=“@style/TvStyle”

android:text=“可怜白发生!” />

</com.scc.demo.view.CustomLayout>

自定义属性

在app/src/main/res/values/attrs.xml中添加属性

<?xml version="1.0" encoding="utf-8"?>

使用自定义属性

  • 在xml中使用

一定要添加:xmlns:test=”schemas.android.com/apk/res-aut…

<com.scc.demo.view.CustomLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:custom=“http://schemas.android.com/apk/res-auto”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:layout_margin=“@dimen/dimen_20”

custom:horizontalSpace=“10dp”

custom:verticalSpace=“20dp”>

</com.scc.demo.view.CustomLayout>

  • 在代码中使用

TypedArray attrArray = context.obtainStyledAttributes(attrs, R.styleable.CustomLayout);

if (attrArray != null) {

//参数1:获取xml中设置的参数;参数2:获取失败2使用参数作为默认值

childHorizontalSpace = attrArray.getDimensionPixelSize(R.styleable.CustomLayout_horizontalSpace, 12);

childVerticalSpace = attrArray.getDimensionPixelSize(R.styleable.CustomLayout_verticalSpace, 12);

MLog.e(getClass().getName(),“HorizontalSpace:”+childHorizontalSpace+“|VerticalSpace:”+childVerticalSpace);

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

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

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

img

img

img

img

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

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

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后的最后

对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的

最后,互联网不存在所谓的寒冬,只是你没有努力罢了!

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

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

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

[外链图片转存中…(img-swx6aQkC-1713838395752)]

最后的最后

对于程序员来说,要学习的知识内容、技术有太多太多,要想不被环境淘汰就只有不断提升自己,从来都是我们去适应环境,而不是环境来适应我们!

当你有了学习线路,学习哪些内容,也知道以后的路怎么走了,理论看多了总要实践的

[外链图片转存中…(img-PATpA9of-1713838395753)]

最后,互联网不存在所谓的寒冬,只是你没有努力罢了!

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 21
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值