2024年Android 手把手教您自定义ViewGroup(一)(2),哔哩哔哩技术类面试

最后我还整理了很多Android中高级的PDF技术文档。以及一些大厂面试真题解析文档。

image

Android高级架构师之路很漫长,一起共勉吧!

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

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

if (i == 1 || i == 3)

{

rHeight += cHeight + cParams.topMargin + cParams.bottomMargin;

}

}

width = Math.max(tWidth, bWidth);

height = Math.max(lHeight, rHeight);

/**

  • 如果是wrap_content设置为我们计算的值

  • 否则:直接设置为父容器计算的值

*/

setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? sizeWidth

width, (heightMode == MeasureSpec.EXACTLY) ? sizeHeight

height);

}

10-14行,获取该ViewGroup父容器为其设置的计算模式和尺寸,大多情况下,只要不是wrap_content,父容器都能正确的计算其尺寸。所以我们自己需要计算如果设置为wrap_content时的宽和高,如何计算呢?那就是通过其childView的宽和高来进行计算。

17行,通过ViewGroup的measureChildren方法为其所有的孩子设置宽和高,此行执行完成后,childView的宽和高都已经正确的计算过了

43-71行,根据childView的宽和高,以及margin,计算ViewGroup在wrap_content时的宽和高。

80-82行,如果宽高属性值为wrap_content,则设置为43-71行中计算的值,否则为其父容器传入的宽和高。

3、onLayout对其所有childView进行定位(设置childView的绘制区域)

// abstract method in viewgroup

@Override

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

{

int cCount = getChildCount();

int cWidth = 0;

int cHeight = 0;

MarginLayoutParams cParams = null;

/**

  • 遍历所有childView根据其宽和高,以及margin进行布局

*/

for (int i = 0; i < cCount; i++)

{

View childView = getChildAt(i);

cWidth = childView.getMeasuredWidth();

cHeight = childView.getMeasuredHeight();

cParams = (MarginLayoutParams) childView.getLayoutParams();

int cl = 0, ct = 0, cr = 0, cb = 0;

switch (i)

{

case 0:

cl = cParams.leftMargin;

ct = cParams.topMargin;

break;

case 1:

cl = getWidth() - cWidth - cParams.leftMargin

  • cParams.rightMargin;

ct = cParams.topMargin;

break;

case 2:

cl = cParams.leftMargin;

ct = getHeight() - cHeight - cParams.bottomMargin;

break;

case 3:

cl = getWidth() - cWidth - cParams.leftMargin

  • cParams.rightMargin;

ct = getHeight() - cHeight - cParams.bottomMargin;

break;

}

cr = cl + cWidth;

cb = cHeight + ct;

childView.layout(cl, ct, cr, cb);

}

}

代码比较容易懂:遍历所有的childView,根据childView的宽和高以及margin,然后分别将0,1,2,3位置的childView依次设置到左上、右上、左下、右下的位置。

如果是第一个View(index=0) :则childView.layout(cl, ct, cr, cb); cl为childView的leftMargin , ct 为topMargin , cr 为cl+ cWidth , cb为 ct + cHeight

如果是第二个View(index=1) :则childView.layout(cl, ct, cr, cb);

cl为getWidth() - cWidth - cParams.leftMargin- cParams.rightMargin;

ct 为topMargin , cr 为cl+ cWidth , cb为 ct + cHeight

剩下两个类似~

这样就完成了,我们的ViewGroup代码的编写,下面我们进行测试,分别设置宽高为固定值,wrap_content,match_parent

5、测试结果


布局1:

<com.example.zhy_custom_viewgroup.CustomImgContainer xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“200dp”

android:layout_height=“200dp”

android:background=“#AA333333” >

<TextView

android:layout_width=“50dp”

android:layout_height=“50dp”

android:background=“#FF4444”

android:gravity=“center”

android:text=“0”

android:textColor=“#FFFFFF”

android:textSize=“22sp”

android:textStyle=“bold” />

<TextView

android:layout_width=“50dp”

android:layout_height=“50dp”

android:background=“#00ff00”

android:gravity=“center”

android:text=“1”

android:textColor=“#FFFFFF”

android:textSize=“22sp”

android:textStyle=“bold” />

<TextView

android:layout_width=“50dp”

android:layout_height=“50dp”

android:background=“#ff0000”

android:gravity=“center”

android:text=“2”

android:textColor=“#FFFFFF”

android:textSize=“22sp”

android:textStyle=“bold” />

<TextView

android:layout_width=“50dp”

android:layout_height=“50dp”

android:background=“#0000ff”

android:gravity=“center”

android:text=“3”

android:textColor=“#FFFFFF”

android:textSize=“22sp”

android:textStyle=“bold” />

</com.example.zhy_custom_viewgroup.CustomImgContainer>

ViewGroup宽和高设置为固定值

效果图:

布局2:

<com.example.zhy_custom_viewgroup.CustomImgContainer xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:background=“#AA333333” >

<TextView

android:layout_width=“150dp”

android:layout_height=“150dp”

android:background=“#E5ED05”

android:gravity=“center”

android:text=“0”

android:textColor=“#FFFFFF”

android:textSize=“22sp”

android:textStyle=“bold” />

<TextView

android:layout_width=“50dp”

android:layout_height=“50dp”

android:background=“#00ff00”

android:gravity=“center”

android:text=“1”

android:textColor=“#FFFFFF”

android:textSize=“22sp”

android:textStyle=“bold” />

<TextView

android:layout_width=“50dp”

android:layout_height=“50dp”

android:background=“#ff0000”

android:gravity=“center”

android:text=“2”

android:textColor=“#FFFFFF”

android:textSize=“22sp”

android:textStyle=“bold” />

<TextView

android:layout_width=“50dp”

android:layout_height=“50dp”

android:background=“#0000ff”

android:gravity=“center”

android:text=“3”

android:textColor=“#FFFFFF”

android:textSize=“22sp”

android:textStyle=“bold” />

</com.example.zhy_custom_viewgroup.CustomImgContainer>

ViewGroup的宽和高设置为wrap_content

效果图:

布局3:

<com.example.zhy_custom_viewgroup.CustomImgContainer xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:background=“#AA333333” >

<TextView

android:layout_width=“150dp”

android:layout_height=“150dp”

android:background=“#E5ED05”

android:gravity=“center”

android:text=“0”

android:textColor=“#FFFFFF”

android:textSize=“22sp”

android:textStyle=“bold” />

<TextView

android:layout_width=“50dp”

android:layout_height=“50dp”

android:background=“#00ff00”

android:gravity=“center”

android:text=“1”

android:textColor=“#FFFFFF”

android:textSize=“22sp”

android:textStyle=“bold” />

<TextView

android:layout_width=“50dp”

android:layout_height=“50dp”

android:background=“#ff0000”

android:gravity=“center”

android:text=“2”

android:textColor=“#FFFFFF”

android:textSize=“22sp”

android:textStyle=“bold” />

<TextView

学习宝典

对我们开发者来说,一定要打好基础,随时准备战斗。不论寒冬是否到来,都要把自己的技术做精做深。虽然目前移动端的招聘量确实变少了,但中高端的职位还是很多的,这说明行业只是变得成熟规范起来了。竞争越激烈,产品质量与留存就变得更加重要,我们进入了技术赋能业务的时代。

不论遇到什么困难,都不应该成为我们放弃的理由!

很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从那里入手去学习,对此我针对Android程序员,我这边给大家整理了一套学习宝典!包括不限于高级UI、性能优化、移动架构师、NDK、混合式开发(ReactNative+Weex)微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!

【Android核心高级技术PDF文档,BAT大厂面试真题解析】

【算法合集】

【延伸Android必备知识点】

【Android部分高级架构视频学习资源】

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

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

微信小程序、Flutter等全方面的Android进阶实践技术;希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!

【Android核心高级技术PDF文档,BAT大厂面试真题解析】

[外链图片转存中…(img-GsjNbKm1-1715585084131)]

【算法合集】

[外链图片转存中…(img-ImiPc58S-1715585084132)]

【延伸Android必备知识点】

[外链图片转存中…(img-8Vsufp1q-1715585084132)]

【Android部分高级架构视频学习资源】

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

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

  • 26
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android自定义ViewGroup是指在Android开发中,通过继承ViewGroup类来创建自定义的布局容器。自定义ViewGroup可以用于实现一些特殊的布局效果,比如侧滑菜单、滑动卡片等等。通过自定义ViewGroup,我们可以更灵活地控制子视图的布局和交互行为,以满足特定的需求。自定义ViewGroup的实现主要包括重写onMeasure()方法和onLayout()方法,来测量和布局子视图。同时,我们还可以通过重写onInterceptTouchEvent()方法和onTouchEvent()方法来处理触摸事件,实现自定义的交互效果。如果你对自定义ViewGroup还不是很了解,或者正想学习如何自定义,可以参考相关的程和文档,如引用\[1\]和引用\[2\]所提到的博客和官方文档。 #### 引用[.reference_title] - *1* [Android 手把手自定义ViewGroup(一)](https://blog.csdn.net/iteye_563/article/details/82601716)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [使用LayoutParams自定义安卓ViewGroup](https://blog.csdn.net/lfq88/article/details/127268493)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Android自定义ViewGroup](https://blog.csdn.net/farsight2009/article/details/62046643)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值