Android之自定义ViewGroup

今天看了鸿洋大神的自定义viewgroup,现在做一下笔记

首先,创建默认构造方法,如下所示
public FlowLayout(Context context) {
super(context,null);
}

public FlowLayout(Context context, AttributeSet attrs) {
super(context, attrs,0);
}

public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

先说一下自定义viewgroup的流程。
onMeasure(用于测量父类的宽高)–>onlayout(用于子类测量以及位置的摆放)

1.重写onMeasure方法:
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//获取父类传递过来的参数, widthMeasureSpec用于获取父容器测量的宽度和测量模式
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);

int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

}

测量模式有以下三种:
MeasureSpec.EXACTLY:对应的是MATCH_PARENT,FILL_PARENT以及精确值,例如50dp之类
MeasureSpec.AT_MOST:对应的是WRAP_CONTENT
MeasureSpec.UNSPECIFIED是未指定尺寸,这种情况不多,一般都是父控件是AdapterView,通过measure方法传入的模式。

接着,遍历所有子view
int cCount = getChildCount();
for (int i=0;i< cCount;i++){
View child = getChildAt(i);
measureChild(child,widthMeasureSpec,heightMeasureSpec);

//MarginLayoutParams从哪里来呢?我们一般会重写generateLayoutParams(AttributeSet attrs)方
//法,如下所示
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
}

@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(),attrs);
}

generateLayoutParams确定了子类的layouparams,如果需要自定义,那么继承ViewGroup.LayoutParams,重写方法即可。

最后,调用setMeasuredDimension(width,height);
将测量到的宽高传入即可。

2.重写onLayout方法
@Override
protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
int childCount = this.getChildCount();
for (int j=0;j< cCount;j++){
View child = getChildAt(j);
LayoutParams lParams = (LayoutParams) child.getLayoutParams();
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
lParams.top + childHeight);
}
}

获取对应的子view,然后对子view进行位置摆放。OVER!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值