当前位置:网站首页>Summary of knowledge points of customized ViewGroup - continuously updated
Summary of knowledge points of customized ViewGroup - continuously updated
2022-06-11 07:56:00 【tinyvampirepudge】
Customize ViewGroup Summary of knowledge points of - Continuous updating
1、child.getMeasuredWidth() Will contain child Of padding value
child Of margin The value of needs to adapt itself .
2、 stay onMeasure In the method :
It needs to be right first child Conduct measure, Then you can get measuredWidth and measureHeight.
There are two commonly used measurement methods :
①ViewGroup#measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec)
however measureChildWithMargins There's a flaw , Its internal MarginLayoutParams It's a strong turn of type , Non empty judgment and type judgment are not added , If we are custom ViewGroup, This may lead to null pointer and type conversion exceptions , To solve this problem , I customized a method , Please see the following for details measureChildWithMarginsAndUsedSpace Method .
3、 measurement child when , in consideration of measureChildWithMargins The defects of , Here we implement a method by ourselves :measureChildWithMarginsAndUsedSpace
protected void measureChildWithMarginsAndUsedSpace(View child,
int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
// The bottom line , Not going to happen
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、 Customize ViewGroup when , You must consider your own padding, as well as child Of padding、margin etc. .
If child It's also custom ViewGroup, Then there are also factors to consider .
5、 How to easily support child Of margin data ? rewrite ViewGroup#generateLayoutParams The method can
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
ViewGroup Provided only ViewGroup.LayoutParams,MarginLayoutParams It's the individual ViewGroup Self inherited .
our ViewGroup To support MarginLayoutParams The best way , Is to rewrite ViewGroup#generateLayoutParams Method .
6、 For some operations that need to limit the width , Can be in onMeasure In the operation .
Measure the actual width first , If you exceed your expectations , And then carry out secondary measurement according to the maximum length .
common FlowLayout Etc , Can be achieved in this way .
To be continued , Coming soon .
边栏推荐
- Bladed入门教程(视频)
- Euler's theorem and its extension (with proof)
- Methods to improve training speed in deep learning and techniques to reduce video memory (suitable for entry-level trick without too many computing resources)
- [software testing] 90% of the interviewers have been brushed out of such resumes
- 安卓初中级开发基础知识整理(面试自用)
- TypeScript-可辨识联合
- Activity中,View#postDelay会导致内存泄漏,但是不会影响Activity的生命周期执行。
- 通过ComponentCallbacks2来接收onTrimMemory等回调,并mock对应的场景
- Modular linear equations (Chinese remainder theorem + general solution)
- How to prepare for the new PMP syllabus exam?
猜你喜欢

Data visualization and Matplotlib

Storage of floating point in memory

Return in foreach and break in for
![[IOT] project management: how to build a better cross functional team?](/img/df/28dbf0f7ba75d1bb3469cc15e70538.png)
[IOT] project management: how to build a better cross functional team?

A detailed explanation of one of the causes of dead loop caused by array out of bounds in C language

学习《缠解论语》

Space geometry

Bladed入門教程(視頻)
![[the most complete ENSP [installation diagram] in history!]](/img/1a/7d50d6e5c06a5e3ce6acab16ddb638.jpg)
[the most complete ENSP [installation diagram] in history!]

SOCKET【5】- struct linger 用法
随机推荐
2021-11-05 definition of cache
Figure seamless database integration tushare interface
[atcoder1981] short diameter (graph theory thinking)
JSP development model
Magnifying mirror rendering
TypeScripy-类的基本使用
Servlet
C language lesson 2
After 4 years of naked resignation from the test, the test post of 15K interview was rubbed on the ground, and the result made me collapse and cry
860. lemonade change
运筹学导论
如何做好空状态设计?来看这份全面总结
【AtCoder2387】+/- Rectangle
Sort - select sort
[atcoder2376] black and white tree (game)
Login and parameterization of interface test
You got 8K in the 3-year function test, but you were actually pretending to work hard
134. gas station
134. 加油站
[cluster] lvs+keepalived high availability cluster