案例仅供参考学习。
MainActivity:
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
TagLayout:
package com.example.test;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class TagLayout extends ViewGroup {
//记录每一行有多高
List<Integer> lineHeights = new ArrayList<Integer>();
List<List<View>> views = new ArrayList<List<View>>();
public TagLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
views.clear();
lineHeights.clear();
//1.计算
//该行有多少列数据
List<View> lineViews = new ArrayList<View>();
int width = getMeasuredWidth();//容器自己的宽度
int lineWidth = 0;
int lineHeight = 0;//这一行的最大高度
int childCount = getChildCount();
for (int j = 0; j < childCount; j++) {
View child = getChildAt(j);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int childWidth = child.getMeasuredWidth();
int childHeigh = child.getMeasuredHeight();
if(childWidth + lp.leftMargin + lp.rightMargin+lineWidth>width){
//超出,换行
lineHeights.add(lineHeight);
views.add(lineViews);
lineWidth = 0;
lineViews = new ArrayList<View>();
}
lineWidth += childWidth + lp.leftMargin +lp.rightMargin;
lineHeight = Math.max(lineHeight, childHeigh + lp.topMargin + lp.bottomMargin);
lineViews.add(child);
}
lineHeights.add(lineHeight);
views.add(lineViews);
int left = 0;
int top = 0;
//2.摆放
int size = views.size();
for (int i = 0; i < size; i++) {
lineViews = views.get(i);
lineHeight = lineHeights.get(i);
for (int j = 0; j < lineViews.size(); j++) {
//遍历这一行的所有child
View child = lineViews.get(j);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
int lc = left + lp.leftMargin;
int tc = top + lp.topMargin;
int rc = lc + child.getMeasuredWidth();
int bc = tc + child.getMeasuredHeight();
child.layout(lc, tc, rc, bc);
left += child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
}
left = 0;
top += lineHeight;
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
int width = 0;//width=所有行里面最宽的一行
int height = 0;//height=所有行的高度相加
//一行的宽度=一行当中的所有view的宽度的和
int lineWidth = 0;
int lineHeight = 0;
//1.测量所有子控件的大小
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
measureChild(child, widthMeasureSpec, heightMeasureSpec);
MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
//子控件真实占用的宽和高度
int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
//当一行放不下的时候需要换行
if(lineWidth + childWidth>sizeWidth){
//换行
width = Math.max(lineWidth, width);
lineWidth = childWidth;
height += lineHeight;
lineHeight = childHeight;
}else{//累加
lineWidth +=childWidth;
lineHeight = Math.max(lineHeight, childHeight);
}
//最后一步
if(i==childCount-1){
width = Math.max(width, lineWidth);
height += lineHeight;
}
}
//2.测量并定义自身的大小
int measuredWidth = (modeWidth == MeasureSpec.EXACTLY)?sizeWidth:width;//wrap_content/match_parent/EXACTLY
int measuredHeight = (modeHeight == MeasureSpec.EXACTLY)?sizeHeight:height;//wrap_content/match_parent/EXACTLY
setMeasuredDimension(measuredWidth, measuredHeight);
}
@Override
protected LayoutParams generateLayoutParams(LayoutParams p) {
// TODO Auto-generated method stub
return new MarginLayoutParams(p);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
// TODO Auto-generated method stub
return new MarginLayoutParams(getContext(), attrs);
}
}
布局:
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<com.example.test.TagLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_margin="5dp"
android:textColor="#f00"
android:background="@drawable/tag_bg"
android:text="你好!" />
<TextView
android:background="@drawable/tag_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_margin="5dp"
android:textColor="#000"
android:text="我好!" />
<TextView
android:background="@drawable/tag_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_margin="5dp"
android:textColor="#f0f"
android:text="大家好!" />
<TextView
android:background="@drawable/tag_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_margin="5dp"
android:textColor="#f0f"
android:text="世界好!" />
<TextView
android:background="@drawable/tag_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_margin="5dp"
android:textColor="#f0f"
android:text="Hello World!" />
<TextView
android:background="@drawable/tag_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_margin="5dp"
android:textColor="#ff0"
android:text="Android你好!" />
</com.example.test.TagLayout>
</LinearLayout>
tag_bg:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#AAC261"/>
<corners android:radius="5dp"/>
<padding android:left="10dp"
android:top="2dp"
android:right="10dp"
android:bottom="2dp" />
</shape>
运行结果:
自定义view的具体步骤:(参考注释)
package com.zero.myviewgroupdemo01;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
public class MyViewGroup extends ViewGroup {
private static final int OFFSET = 100;//表示缩进的尺寸
public MyViewGroup(Context context) {
super(context);
}
public MyViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//1. 测量自身
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 2. 为每个子View计算测量的限制信息 Mode / Size
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//3. 把上一步确定的限制信息,传递给每一个子View,然后子View开始measure
//自己的尺寸
int childCount = getChildCount();//子view的个数
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
ViewGroup.LayoutParams lp = child.getLayoutParams();
int childWidthSpec = getChildMeasureSpec(widthMeasureSpec, 0, lp.width);
int childHeightSpec = getChildMeasureSpec(heightMeasureSpec, 0, lp.height);
child.measure(childWidthSpec, childHeightSpec);
}
int width = 0;
int height = 0;
//4. 获取子View测量完成后的尺寸
//5. ViewGroup根据自身的情况,计算自己的尺寸
switch (widthMode) {
case MeasureSpec.EXACTLY:
width = widthSize;
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.UNSPECIFIED:
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
int widthAddOffset = i * OFFSET + child.getMeasuredWidth();
width = Math.max(width, widthAddOffset);//取最大的宽度
}
break;
default:
break;
}
switch (heightMode){
case MeasureSpec.EXACTLY:
height =heightSize;
break;
case MeasureSpec.AT_MOST:
case MeasureSpec.UNSPECIFIED:
for (int i = 0; i < childCount; i++) {
View child = getChildAt(i);
height += child.getMeasuredHeight();
}
break;
default:
break;
}
//6. 保存自身的尺寸
setMeasuredDimension(width,height);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// 1. 遍历子View for
// 2. 确定自己的规则
// 3. 子View的测量尺寸
// 4. left,top,right,bottom
// 6. child.layout
int left = 0;
int top = 0;
int right = 0;
int bottom = 0;
int childCount = getChildCount();
for(int i = 0; i < childCount; i++){
View child = getChildAt(i);
left = i * OFFSET;
right = left + child.getMeasuredWidth();
bottom = top + child.getMeasuredHeight();
child.layout(left,top,right,bottom);
top += child.getMeasuredHeight();//不考虑padding margin gravity xxx
}
}
}