不讲了,就是给自定义的控件添加自定义属性。
步骤大概就是
-
在 attrs下添加
<declare-styleable>
标签,指明对应的控件,然后在下面声明属性的名称和类型。 -
在xml中就可以在使用控件时使用该自定义属性。
-
代码在该控件中 ,通过第三个构造函数的TypedArray来获取对应的属性的值,然后就可以对该属性进行操作就ok了。
View和ViewGroup的绘制流程的三大流程
balabalabala。。。
在给Viewgroup加margin时,如果在onLayout()里面加,那就也要在onMeasure()里加。否则会导致Container太小而控件显示不全的问题。
我们可以重写 generateLayoueParams()和generateDefaultLayoutParams()函数,在里面直接返回MarginLayoutParams()
只有重写generateLayoueParams()函数才能获取到控件的margin值。
这是因为在contain中初始化子控件时,会调用generateLayoueParams§来为子控件生成对应的布局属性,但默认只生成layout_width和layout_height,即正常情况下generateLayoueParams()得到的LayoutParams是得不到margin值的。而MarginLayoutParams是派生自LayoutParams的,所以我们在LayoutParams强转MarginLayoutParams是不会报错的。
首先,这个FlowLayout中有多个TextView,我们在style中定义他们:
然后xml如下:
<com.example.myviewdomo.diy.FlowLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:background=“@android:color/white”>
<TextView
style=“@style/text_flag_01”
android:text=“你好” />
。。。
</com.example.myviewdomo.diy.FlowLayout>
这里注意:我们为FlowLayout的宽度设为match_parent,高度设为wrap_content
1、提取margin值与重写onMeasure
我们要提取margin值,就一定要重写generateLayout()函数:
@Override
protected LayoutParams generateLayoutParams(LayoutParams p) {
return new MarginLayoutParams§;
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
然后重写onMeasure()函数去计算当前FlowLayout所占的区域大小。
要实现FlowLayout,必然涉及下面几个问题:
(1)什么时候换行:
FlowLayout是一行行的,所以当前行已经放不下下一控件时,就将该控件放到下一行显示。所以需要一个变量来计算当前行已经占据的宽度,以判断剩下的空间是否还能容得下下一个控件
(2)如何得到FlowLayout宽度
宽度是所有行宽度的最大值,所以我们每记录一行的时候就要将做最大行宽度的比较
(3)如何得到FLowLayout高度
高度是每一行总和,每一行的高度是其中控件高度的最大值。
我们先利用MeasureSpec获取系统建议的数值和模式。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
}
然后计算FlowLayout所占用区域的大小,先定义几个变量:
//记录每一行的宽度
int lineWidth = 0;
//记录每一行的高度
int lineHeight = 0;
//记录整个Layout的高度
int height = 0;
//记录整个Layout的宽度
int width = 0;
再开始计算:
int count = getChildCount();
for (int i = 0; i < count; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
if (lineWidth + childWidth > measureWidth) {
//需要换行
height += lineHeight;
//因为当前行放不下当前空间,而将此控件调到下一行,所以将此控件的高度和宽度初始化给lineHeight、lineWidth
lineHeight = childHeight;
lineWidth = childWidth;
} else {
//否则累加lineWidh,lineHeight取最大值
lineHeight = Math.max(lineHeight, childHeight);
lineWidth += childWidth;
}
//因为最后一行是不会超出width范围的,所以需要单独处理
if (i == count - 1) {
height += lineHeight;
width = Math.max(width, lineWidth);
}
}
setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? measureWidth : width, heightMode == MeasureSpec.EXACTLY ? measureHeight : height);
上面代码中:
我们开启循环遍历子View并用下面代码:
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
来获取和计算每个子View的宽高。
然后用通过该子View的宽高,来计算FlowLayout的宽高和要不要换行。
if (lineWidth + childWidth > measureWidth) {
//需要换行
height += lineHeight;
//因为当前行放不下当前空间,而将此控件调到下一行,所以将此控件的高度和宽度初始化给lineHeight、lineWidth
lineHeight = childHeight;
lineWidth = childWidth;
} else {
//否则累加lineWidh,lineHeight取最大值
lineHeight = Math.max(lineHeight, childHeight);
lineWidth += childWidth;
}
需要注意的是,在计算最后一行时,肯定是不会超过行宽的,而我们在for循环中,当不超过行宽时只做了如下处理:
} else {
//否则累加lineWidh,lineHeight取最大值
lineHeight = Math.max(lineHeight, childHeight);
lineWidth += childWidth;
}
我们只计算了行高和行宽,没有记录整个layout的宽和高
所以到了最后一个控件的时候,我们需要单独计算width 和height
//因为最后一行是不会超出width范围的,所以需要单独处理
if (i == count - 1) {
height += lineHeight;
width = Math.max(width, lineWidth);
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此我收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
[外链图片转存中…(img-5O1XTAYU-1715487593201)]
[外链图片转存中…(img-aDKvndVH-1715487593203)]
[外链图片转存中…(img-doLGiDV2-1715487593205)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点!不论你是刚入门Android开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!