安卓自定义控件(四)实现自定义Layout

时间:2023-03-09 12:50:15
安卓自定义控件(四)实现自定义Layout

本来我是不准备写这篇文章的,我实在想不出有什么样奇怪的理由,会去继承ViewGroup然后自定义一个布局,大概是我的项目经验还不够吧,想了好久,想到了这样一个需求:

需求

安卓自定义控件(四)实现自定义Layout
如图:在项目中经常有一个这样的需求,一个View靠左,另一个View靠右,这样的布局方式使用频率非常高,通常使用的是RelativeLayout,但是每次设置Align属性真的好烦,现在就来实现一下这样的布局,解决这个问题。

思路

其实具体实现一个布局,还是可以分为两种:
1.继承一个具体的Layout,这样可以省去很多的代码逻辑,只需要注意自己要处理的业务即可,就比如说Listview嵌套Listview到时候,我们会考虑重写它的onMeasure方法。
2.继承自ViewGroup,然后自己设计代码逻辑。

LayoutParams

之前在介绍onMeasure的时候,我们注意到一个特殊的类MeasureSpec,那onLayout中,我们要注意什么参数呢?答案是:LayoutParams,这个单词直译是布局参数,从名字就可以看出是和布局相关的,既然说onLayout是用于确定子View位置的方法,那么自然离不开它的使用。

LayoutParams用于确定支持childView支持哪些属性,比如LinearLayout有一个LinearLayout.LayoutParams,这个LayoutParams就提供了Gravity,Weight这两个属性。而下面这段代码,用到了MarginLayoutParams它为子View提供了Margin属性。

源码:

这里使用第二种方式实现,也就是直接继承ViewGroup,因为我的需求是一个布局,它只允许有两个子View,一个在左边,一个在右边,这个不同于其它任何一个布局。

/**
* 自定义布局
* Created by ChenSS on 2016/12/3.
*/
public class MyRelativeLayout extends ViewGroup {
public MyRelativeLayout(Context context) {
this(context, null);
} public MyRelativeLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
} public MyRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
} @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
int childWidth;
int childHeight;
MarginLayoutParams params; for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
childWidth = childView.getMeasuredWidth();
childHeight = childView.getMeasuredHeight();
params = (MarginLayoutParams) childView.getLayoutParams(); int left = 0, top = 0, right = 0, bottom = 0; switch (i) {
case 0:
left = params.leftMargin;
top = params.topMargin;
break;
case 1:
left = getWidth() - childWidth - params.rightMargin;
top = params.topMargin;
break;
}
right = left + childWidth;
bottom = childHeight + top;
childView.layout(left, top, right, bottom);
}
} @Override
public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
//指定我们所需的LayoutParams
return new MarginLayoutParams(getContext(), attrs);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec); // 计算出所有的childView的宽和高
measureChildren(widthMeasureSpec, heightMeasureSpec); // wrap_content时设置的宽和高
int width = 0;
int height = 0; int childCount = getChildCount();
if (childCount > 2)
throw new IllegalArgumentException("too many childView"); int childWidth;
int childHeight;
MarginLayoutParams params;
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
params = (MarginLayoutParams) childView.getLayoutParams(); //宽度为子View大小总和
childWidth = childView.getMeasuredWidth() + params.leftMargin + params.rightMargin;
width += childWidth; //高度取最高的那个子View
childHeight = childView.getMeasuredHeight() + params.topMargin + params.bottomMargin;
height = childHeight > height ? childHeight : height;
} /**
* 根据MeasureSpec设置MyRelativeLayout的宽高
*/
setMeasuredDimension((widthMode == MeasureSpec.EXACTLY) ? sizeWidth : width,
(heightMode == MeasureSpec.EXACTLY) ? sizeHeight : height);
}
}

安卓自定义控件(四)实现自定义Layout

参考:Android 手把手教您自定义ViewGroup

等到什么时候有个好的创意,再改成原创o(∩_∩)o~