流式布局

来源:互联网 发布:数据库关键字查询 编辑:程序博客网 时间:2024/06/08 20:11
自定义一个控件  使这个控件中的所有子控件按照自己的想法和需求排列<?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.liushi.MainActivity"><com.example.liushi.liu    android:layout_width="wrap_content"    android:layout_height="wrap_content">    <TextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="今天是个好日子"    />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="心想的事儿都能成"        /></com.example.liushi.liu></android.support.constraint.ConstraintLayout>

//////////////////////////////////////////////////////////////////////////////////////////////
package com.example.jingdong.zdyview;import android.content.Context;import android.graphics.Canvas;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;/** * Created by Administrator on 2017/12/16 0016. */public class LiuShi extends ViewGroup {    public LiuShi(Context context) {        this(context,null);    }    public LiuShi(Context context, AttributeSet attrs) {        this(context, attrs,0);    }    public LiuShi(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    //测量    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);    //测量子view        measureChildren(widthMeasureSpec,heightMeasureSpec);    }    //绘制的方法    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        //初始化子view的宽高        int wid=0; int hei=0;        //得到子view的个数        int childcount=getChildCount(); for(int i=0;i<childcount;i++) {        //得到这个viewgroup里面的每一个子view         View childView=getChildAt(i);        //得到每一个子view的宽高        int width=childView.getMeasuredWidth();        int height=childView.getMeasuredHeight();        //确定子view的高度        childView.layout(wid,hei,width,hei+height);        //累加所有子view宽高        wid=width; hei+=height;        }    }}
原创粉丝点击