80行代码完成自动换行layout

这篇博客介绍了一个简洁的自动换行布局,适用于只需要基本功能和间距控制的场景。作者强调不需要复杂的代码和额外的属性,通过horizontalSpace和verticalSpace属性控制间距,并展示了动态添加和XML写死的显示效果。对于点击事件,建议由添加的控件自身处理。
摘要由CSDN通过智能技术生成
网上有很多的自动换行的layout了,但看了一圈,感觉很多人都把问题想复杂了,这也就导致了代码的复杂。有些时候你想要的只是一个可以自动换行的layout,可以设置横竖间距,不需要其他一些奇奇怪怪的功能和设置一些奇奇怪怪的属性。那么你可以考虑我写的这个
public class AutoLineLayout extends ViewGroup {
   
   private int horizontalSpace;

   private int verticalSpace;

   private int theRealWidth;

   public AutoLineLayout(Context context, AttributeSet attrs) {
   
       super(context, attrs);
       TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.AutoLineLayout);
       horizontalSpace= (int) Math.ceil(array.getDimension(R.styleable.AutoLineLayout_horizontalSpace,0));
       verticalSpace=(int) Math.ceil(array.getDimension(R.styleable.AutoLineLayout_verticalSpace,0));
       array.recycle();
   }

   @Override
   protected void onMeasure(int widthMeasureSpec, int heightMeasureSpe
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值