当前位置:网站首页>自定义ViewGroup的知识点总结-持续更新
自定义ViewGroup的知识点总结-持续更新
2022-06-11 07:47:00 【tinyvampirepudge】
自定义ViewGroup的知识点总结-持续更新
1、child.getMeasuredWidth()中会包含child的padding值
child的margin的值需要自行适配。
2、在onMeasure方法中:
需要先对child进行measure,然后才能获取到measuredWidth和measureHeight。
常用的测量方法有两个:
①ViewGroup#measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec)
但是measureChildWithMargins有个缺陷,它内部的MarginLayoutParams是类型强转而来的,没有添加非空判断和类型判断,如果我们是自定义ViewGroup,这里会就有可能导致空指针和类型转换异常,为了解决这个问题,我自定义了一个方法,具体请看下面的measureChildWithMarginsAndUsedSpace方法。
3、测量child时,考虑到measureChildWithMargins的缺陷,这里自行实现了一个方法:measureChildWithMarginsAndUsedSpace
protected void measureChildWithMarginsAndUsedSpace(View child,
int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
// 兜底,不会发生
if (child.getLayoutParams() == null) {
return;
}
final LayoutParams lp = child.getLayoutParams();
int paddingH = getPaddingLeft() + getPaddingRight() + widthUsed;
int paddingV = getPaddingTop() + getPaddingBottom() + heightUsed;
if (child.getLayoutParams() instanceof MarginLayoutParams) {
final MarginLayoutParams mlp = (MarginLayoutParams) child.getLayoutParams();
paddingH += mlp.leftMargin + mlp.rightMargin;
paddingV += mlp.topMargin + mlp.bottomMargin;
}
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, paddingH, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, paddingV, lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
4、自定义ViewGroup时,必须考虑自身的padding,以及child的padding、margin等。
如果child也是自定义ViewGroup,那么也需要考虑因素。
5、如何方便的支持child的margin数据?重写ViewGroup#generateLayoutParams方法即可
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
ViewGroup只提供了ViewGroup.LayoutParams,MarginLayoutParams是各个ViewGroup自行继承实现的。
我们的ViewGroup要支持MarginLayoutParams最好的方式,就是重写ViewGroup#generateLayoutParams方法。
6、对于一些需要限制宽度的操作,可以在onMeasure中操作。
先测量出实际宽度,如果超出了预期,再根据其最大长度进行二次测量即可。
常见的FlowLayout等自定义布局,都可以这么实现。
未完待续,敬请期待。
边栏推荐
- 放大镜子效果图
- Simple use of string
- Implementation of queue (C language)
- 【AtCoder1980】Mysterious Light(数学模拟)
- Rabin-Miller素数测试
- [atcoder1983] BBQ hard (combination number + clever model transformation)
- C language function stack frame
- Remote office experience | community essay solicitation
- 零基础自学SQL课程 | OUTER JOIN外连接
- 【AtCoder1983】BBQ Hard (组合数+巧妙模型转化)
猜你喜欢

安卓初中级开发基础知识整理(面试自用)

Tidb Cloud est en ligne sur le marché Google Cloud pour permettre aux développeurs du monde entier d'utiliser une nouvelle pile de bases de données htap en temps réel

Xshell7 和 Xftp7要继续使用此程序,您必须应用最新的更新或者使用新版本

Detailed explanation of shift operator and bit operator in C language

Summary of evaluation index knowledge points in target detection: summary of IOU cross overlap unit and map/ap/tp/fp/np

C language function stack frame

How to output the percent sign "%" in printf function in C language

Understanding of Poisson distribution and Poisson process and Erlang distribution and their relations (important theories in queuing theory and operational research)

mpi

SOCKET【5】- struct linger 用法
随机推荐
Implementation of queue (C language)
学习《缠解论语》
[atcoder1998] stamp Rally
20200727 T2 small w playing game [generating function (binomial inversion technique)]
2022.6.6 特长生模拟
C language - growth diary-04- preliminary exploration of local variables (local variables)
[IOT] intelligent hardware: how to obtain the WiFi signal strength of hardware products
【AtCoder2307】Tree Game(博弈)
TiDB Cloud 上線 Google Cloud Marketplace,以全新一棧式實時 HTAP 數據庫賦能全球開發者
.NET C#基础(6):命名空间 - 有名字的作用域
134. 加油站
20200802 T3 I always like [generating function exclusion, Lagrangian inversion]
测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...
MFC debugger OutputDebugString override
Sort - select sort
Nim product
【集群】LVS+keepalived高可用集群
El expressions and JSTL
Classes and objects (medium)
C language to achieve a simple game - minesweeping