android空间超出viewgroup,自定义ViewGroup实现水平布局空间不足自动换行的效果

0818b9ca8b590ca3270a3433284dd417.png

0818b9ca8b590ca3270a3433284dd417.png

今天带着大家一起来实现这样的一个效果,当水平方向空间不足无法显示下一个子View时自动换行,看到这样的效果首先就会想到自定义viewgroup,那么该如何实现呢,好,废话不好说。看代码:

public class MyViewGroup extends ViewGroup {

public MyViewGroup(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}

public MyViewGroup(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public MyViewGroup(Context context) {

this(context, null);

}

private int DEFAULT_WIDTH = 200;//默认的宽度

private int DEFAULT_HEIGHT = 200;//默认的高度

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int width = getSize(DEFAULT_WIDTH, widthMeasureSpec);

int height = getSize(DEFAULT_HEIGHT, heightMeasureSpec);

measureChildren(widthMeasureSpec, heightMeasureSpec);

setMeasuredDimension(width, height);

}

private int getSize(int defaultSize, int widthMeasureSpec) {

int result = 0;

int mode = MeasureSpec.getMode(widthMeasureSpec);

int size = MeasureSpec.getSize(widthMeasureSpec);

if (mode == MeasureSpec.EXACTLY) {

result = size;

} else if (mode == MeasureSpec.AT_MOST) {

result = Math.min(defaultSize, size);

} else {

result = defaultSize;

}

return result;

}

@Override

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

int mWidth = getMeasuredWidth();

int childX = l;

int childY = t;

for (int index = 0; index < getChildCount(); index++) {

View view = getChildAt(index);

int childWidth = view.getMeasuredWidth();

int childHeight = view.getMeasuredHeight();

MyViewGroup.LayoutParams param = (LayoutParams) view

.getLayoutParams();

if (childX + childWidth + param.leftMargin + param.rightMargin > mWidth) {// 如果宽度大于父控件的宽度,则换行

childY += childHeight + param.topMargin;

childX = l;

}

view.layout(childX + param.leftMargin, childY + param.topMargin,

childX + childWidth + param.leftMargin, childY

+ childHeight + param.topMargin);

childX += childWidth + param.leftMargin + param.rightMargin;

}

}

public static class LayoutParams extends ViewGroup.MarginLayoutParams {

public LayoutParams(Context c, AttributeSet attrs) {

super(c, attrs);

}

}

@Override

public LayoutParams generateLayoutParams(AttributeSet attrs) {

return new MyViewGroup.LayoutParams(getContext(), attrs);

}

}

布局文件:

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity" >

android:id="@+id/myViewGroup"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#aabbcc" >

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="10dp"

android:background="#aacc00" />

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="10dp"

android:background="#aa00ff" />

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="10dp"

android:background="#aa0000" />

android:layout_width="300dp"

android:layout_height="100dp"

android:layout_margin="10dp"

android:background="#aaff00" />

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="10dp"

android:background="#ff0000" />

android:layout_width="150dp"

android:layout_height="100dp"

android:layout_margin="10dp"

android:background="#000000" />

android:layout_width="100dp"

android:layout_height="100dp"

android:layout_margin="10dp"

android:background="#aa00dd" />

ManActivity:

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

}

OK,效果实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值