简单流式布局

效果图
主布局的类不用更改

自定义类
package com.example.zdy_view4;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * author:Created by MingShao on 2017/11/30.
 */

public class Myviewgroup extends ViewGroup {
    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 onLayout(boolean b, int l, int i1, int i2, int i3) {
        int currentwidth=0;
        int currentHeight=0;
        int childCount=getChildCount();
        for(int i= 0;i<childCount;++i){
            View view = getChildAt(i);
            int width = view.getMeasuredWidth();
            int height = view.getMeasuredHeight();
            view.layout(currentwidth,currentHeight,currentwidth+width,currentHeight+height);
            currentwidth+=width;
            currentHeight+=height;
        }
    }

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

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }
}
主布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zdy_view4.MainActivity">

    <com.example.zdy_view4.Myviewgroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绝望"
            android:padding="10dp"
            android:textSize="20sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绝望"
            android:padding="10dp"
            android:textSize="20sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绝望"
            android:padding="10dp"
            android:textSize="20sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绝望"
            android:padding="10dp"
            android:textSize="20sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绝望"
            android:padding="10dp"
            android:textSize="20sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绝望"
            android:padding="10dp"
            android:textSize="20sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绝望"
            android:padding="10dp"
            android:textSize="20sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绝望"
            android:padding="10dp"
            android:textSize="20sp"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="绝望"
            android:padding="10dp"
            android:textSize="20sp"
            />

    </com.example.zdy_view4.Myviewgroup>
</android.support.constraint.ConstraintLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值