自定义自动换行布局控件,实现子控件宽度超出整行时自动换行。setMeasuredDimension的使用

本文介绍如何创建一个自定义布局控件,当子控件宽度超出一行时能自动换行。通过重写测量方法setMeasuredDimension,确保内容完整显示,避免LinearLayout和RelativeLayout的局限性。在布局过程中,将此控件视作RelativeLayout使用。
摘要由CSDN通过智能技术生成

在开发应用时,很多时候都遇到动态加载标签,但是内容不确定的情况,常用的几种布局在此时就不太好用了,如LinearLayout只能单行或者单列进行布局,而RelativeLayout在定位时需要确定子控件的大小和相对位置,因此需要自己进行一些重写来实现自行换行。

public class FlowLayout extends RelativeLayout {
        int mLeft, mRight, mTop, mBottom, currentBottom;
        Hashtable<View, Position> map = new Hashtable<View, FlowLayout.Position>();

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

        }

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

        }

        public FlowLayout(Context context) {
                super(context);

        }

        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
                // TODO Auto-generated method stub
                int count = getChildCount();
        
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值