Android自动换行布局

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

/***/
public class AutoWrapLayout extends ViewGroup {
  //支持的最大行数 10行,可以根据需要调整
    private int[] mRawArray = new int[10];

    private int mPadding = 3;

    public AutoWrapLayout(Context context) {
        this(context, null);
    }

    public AutoWrapLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

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

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

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int width = getMeasuredWidth();
        int height = getMeasuredHeight();

        int childCount = getChildCount();

        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        }

        int rawChildWidthSum = 0;
        int maxChildHeight = 0;

        for (int i = 0, raw = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                int childWidth = child.getMeasuredWidth();
                int childHeight = child.getMeasuredHeight();

                if (maxChildHeight < childHeight) maxChildHeight = childHeight;
                rawChildWidthSum += childWidth;

                mRawArray[raw] = i;
                if (rawChildWidthSum > width) {
                    mRawArray[raw]--;
                    raw++;
                    rawChildWidthSum = 0;
                }
                mRawArray[raw] = i;
            }
        }

        int allRawHeight = 0;

        for (int aRawArray : mRawArray) {
            if (aRawArray > 0) {
                allRawHeight += (maxChildHeight + mPadding * 3);
            } else {        break       }     }

        setMeasuredDimension(width, allRawHeight);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int left = 0;
        int top = t;

        int maxChildHeight = 0;

        for (int i = 0; i < mRawArray.length; i++){//every raw
            //i <=> raw number
            int aRawArray = mRawArray[i];

            // Handle first raw particularly.
            if (i == 0) {
                if (aRawArray > 0) {
                    for (int j = 0; j <= aRawArray; j++) {// column
                        final View child = getChildAt(j);

                        int childWidth = child.getMeasuredWidth();
                        int childHeight = child.getMeasuredHeight();

                        left += childWidth;
                        top = 0;

                        if (maxChildHeight < childHeight) maxChildHeight = childHeight;

                        child.layout(left - childWidth, 2, left, top +  maxChildHeight + 3);
                    }
                }
                continue;
            }

            if (aRawArray > 0) {
                left = 0;
                for (int j = mRawArray[i - 1] + 1; j <= aRawArray; j++) {// column
                    final View child = getChildAt(j);

                    int childWidth = child.getMeasuredWidth();
                    int childHeight = child.getMeasuredHeight();

                    left += childWidth;
                    top = i * maxChildHeight + maxChildHeight + t;

                    if (maxChildHeight < childHeight) maxChildHeight = childHeight;

                    child.layout(left - childWidth, top - maxChildHeight + 2, left, top + 3);
                }
            } else {
                break;
            }
        }

    }
}
时间: 2024-08-27 00:04:39

Android自动换行布局的相关文章

Android开发布局简单介绍

Android开发布局介绍 1.线性布局 LinearLayout 布局中的组件会一个挨着一个排列起来,android:orientation属性可以控制排列方向,horizontal-水平.vertical-垂直 线性布局不会自动换行,当超出屏幕范围时,剩下的组件不会显示出来. 2.表格布局 TableLayout 继承自LinearLayout,其本质依然是LinearLayout.通过TableRow来管理表格的行数和列数.添加一个TableRow就是一行. 3.帧布局 FrameLayo

Android——ListView布局+适配器(三)

Android--ListView布局+适配器(三) package com.example.administrator.newstop; import android.os.Bundle; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import andro

android 在布局中动态添加控件

第一步 Java代码 final LayoutInflater inflater = LayoutInflater.from(this); 第二步:获取需要被添加控件的布局 Java代码 final LinearLayout lin = (LinearLayout) findViewById(R.id.LinearLayout01); 第三步:获取需要添加的布局(控件) Java代码 LinearLayout layout = (LinearLayout) inflater.inflate( R

Android可伸缩布局-FlexboxLayout(支持RecyclerView集成)

Android可伸缩布局-FlexboxLayout(支持RecyclerView集成) 1 . 前言 前几天看到Google官方的博客介绍了Google开源的一个强大的布局-FlexboxLayout,看见第一眼我心里的想法是,卧槽,Android 居然有这么一个强大的布局.作为一个有好奇心的工程狮,当然第一时间就去试了试手,效果非常赞,因此这篇文章就介绍一下它的用法和最新版添加的一些特性(支持集成RecyclerView),Github地址:https://github.com/google

浅谈Android五大布局

Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面.Android的五大布局分别是LinearLayout(线性布局).FrameLayout(单帧布局).RelativeLayout(相对布局).AbsoluteLayout(绝对布局)和TableLayout(表格布局). LinearLayout: LinearLayout按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后

Android表格布局之设置边框

Android表格布局本身没有边框,不过可以通过背景色的设置可以实现表格边框的显示. 首先可以设置TableRow的背景色,然后设置内容的背景色.根据它们的颜色差就出现了边框.只要微调Content与TableRow的margin和pading属性就可以了! 调的过程真是烦人!下次不做这种工作了~呜呜!难受! 贴上布局代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xml

iOS中xib与storyboard原理,与Android界面布局的异同

用文本标记语言来进行布局,用的最多的应该是HTML语言.HTML可以理解为有一组特殊标记的XML语言. 一.iOS中xib与storyboard显示原理 在iOS中主要的布置界面的方式有3种:代码,xib,storyboard. 1. 代码 代码布置界面是万能的,但通常很复杂.布置一个简单的界面可能需要很多行代码,因此十分繁琐. 下面为创建一个按钮的代码,最少也要3行: UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd

[转]Android:布局实例之模仿QQ登录界面

Android:布局实例之模仿QQ登录界面 预览图: 准备: 1.找到模仿对象 QQ登陆界面UI下载>>>>> 2.导入工程 3.查看布局结构和使用控件 其对应效果图分布为 4.分析样式选择器 下拉箭头2种样式:点击和默认状态 文本框2种样式:聚焦和默认状态 复选框3种样式:选择.不选择和鼠标点着不放 左下角按钮2种样式:点击和默认 登录按钮2样式:点击和默认 ============================================帖代码===========

Android五大布局Layout

 Android开发中,我们可能会遇到过一些很复杂的布局,对于初学者来说,可能脑子会嗡的一下,"这么复杂!该怎么整?!". 不要担心!再复杂的布局其实也是由简单地布局组成的,我们要学会将它分解成基本的布局,那么问题就迎刃而解了. Android共有五种常见布局方式,分别是:LinearLayout(线性布局),FrameLayout(单帧布局),AbsoluteLayout(绝对布局),RelativeLayout(相对布局),TableLayout(表格布局). 下面首先看一下这