自定义Viewgroup实现流式布局(3):条件换行的自定义ViewGroup

https://blog.csdn.net/shb2058/article/details/87189763

上篇已经实现了条件换行的自定义VIew,最后也说到onLayout来设置换行显示,这里用ViewGroup来实现,无非canvas.drawText控制文字换行换成onLayout来控制textview的换行。

既然是ViewGroup,那里面装的就是View了,这里使用上篇自定义的textview,因为系统的Textview是有行距的。
首先,onMeasure还是会首先执行,你可以使用自定义View时候的方法来计算宽高(文字大小+padding)+viewgroup的padding,也可以直接得到子View的宽高,然后加上padding。
得到子view的个数: int hadChildCount = getChildCount();
得到子view的实例:   View child = getChildAt(i);
计算子view的宽高等: measureChild(child, widthMeasureSpec, heightMeasureSpec);
得到子view的宽高:int childMeasuredHeight = child.getMeasuredHeight();
onMeasure已经得到了当前viewgroup的所有view,现在需要对子view的位置进行排布
使用View.layout(left,top,right,bottom)设置位置即可:四个点确定一个矩形范围
 

效果图:

 

实例代码如下:

public class MyTextview_Viewgroup extends ViewGroup {
    public MyTextview_Viewgroup(Context context) {
        super(context);
    }

    public MyTextview_Viewgroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAtters(context, attrs);
    }

    public MyTextview_Viewgroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAtters(context, attrs);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MyTextview_Viewgroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initAtters(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int startLeft = getPaddingLeft();
        int startTop = getPaddingTop();
        int startRight = getPaddingLeft();
        int startBottom = getPaddingTop();
        for (int i = 0; i < childs.size(); i++) {
            View child = childs.get(i);
            startRight = startRight + child.getMeasuredWidth();
            startBottom = startBottom + child.getMeasuredHeight();
            //left top right bottom
            child.layout(startLeft, startTop, startRight, startBottom);
            startTop = startTop + child.getMeasuredHeight();

        }
    }

    String text = "需要显示的文字";

    private void initAtters(Context context, @Nullable AttributeSet attrs) {
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyTextview);
        text = array.getString(R.styleable.MyTextview_texcontent);

    }

    private void initView(int widthSize) {
        //因为onmeasure会执行多次
        this.removeAllViews();
        //首先还是判断能不能一行放下
        Paint paint = null;
        Rect rect = null;
        if (paint == null) {
            paint = new Paint();
            paint.setTextSize(SizeUtil.sp2px(getContext(), 20.0f));
            paint.setColor(Color.parseColor("#000000"));
            paint.setAntiAlias(true);
            paint.setFilterBitmap(true);//位图过滤
            ;
        }
        if (rect == null) {
            rect = new Rect();
        }
        paint.getTextBounds(text, 0, text.length(), rect);
        //
        if (widthSize - getPaddingLeft() - getPaddingRight() >= rect.width()) {
            //一行容纳
            MyTextview textView = new MyTextview(getContext());
            textView.setPadding(0, 0, 0, 0);
            ViewGroup.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            textView.setLayoutParams(layoutParams);
            textView.setText(text);
            this.addView(textView);
            return;
        }

        List<String> rowStrings = new ArrayList<>();
        double rows = text.length() / 7.0;
        int lineNum = (int) Math.ceil(rows);
        //一行文字所包含的文字数
        //0开始,依次加然后判断
        for (int i = 0; i < lineNum; i++) {
            String rowText = "";
            if (i == lineNum - 1) {
                rowStrings.add(text.substring(i * 7, text.length()));
                rowText = text.substring(i * 7, text.length());
            } else {
                rowStrings.add(text.substring(i * 7, (i + 1) * 7));
                rowText = text.substring(i * 7, (i + 1) * 7);
            }
            MyTextview textView = new MyTextview(getContext());
            textView.setPadding(0, 0, 0, 0);
            ViewGroup.LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            textView.setLayoutParams(layoutParams);
            textView.setText(rowText);
            this.addView(textView);
        }

    }

    List<View> childs;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        childs = new ArrayList<>();
        //       Log.e("MyViewgroup+onMeasure", "现在有多少个子view" + getChildCount());
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);   //获取宽的模式
        int heightMode = MeasureSpec.getMode(heightMeasureSpec); //获取高的模式
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);   //获取宽的尺寸
        int heightSize = MeasureSpec.getSize(heightMeasureSpec); //获取高的尺寸
        initView(widthSize);
        //测量子view的宽高,然后得到整个的宽高
        int width = 0;
        int height = 0;

        int hadChildCount = getChildCount();
        for (int i = 0; i < hadChildCount; i++) {
            View child = getChildAt(i);
            childs.add(child);
            //测量下子view的大小
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            int childMeasuredHeight = child.getMeasuredHeight();
            int childMeasuredWidth = child.getMeasuredWidth();
            // Log.e("当前child的宽和高是", childMeasuredWidth + "-" + childMeasuredHeight);
            if (childMeasuredWidth > width) {
                width = childMeasuredWidth + getPaddingLeft() + getPaddingRight();
            }
            height = height + childMeasuredHeight;
        }
        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {

        }
        if (heightMode == MeasureSpec.EXACTLY) {
            height = heightSize;
        }
        //保存测量宽度和测量高度
        setMeasuredDimension(width, height + getPaddingBottom() + getPaddingTop());
        Log.e("计算出来viewgroup的高度是", height + getPaddingTop() + getPaddingBottom() + "");
    }


}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进课程实践、课外项目或毕业设计。通过分析和运源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值