实现Android流式布局

Android 组件 —— 流式布局 这是一个Android流式布局,根据鸿翔大神之前写的FlowLayout设计思路结合Material库中的ChipGroup设计架构衍生而来,全新的流式布局组件。 阅读详情
  • 自定义View
package com.example.coder.myapplication;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Coder on 2017/4/7.
 */

public class FlowLayout extends ViewGroup {
    public FlowLayout(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        //
    }

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        // wrap_content
        int width = 0;
        int height = 0;

        // 记录每一行的宽度与高度
        int lineWidth = 0;
        int lineHeight = 0;

        // 得到内部元素的个数
        int cCount = getChildCount();

        for (int i = 0; i < cCount; i++)
        {
            View child = getChildAt(i);
            // 测量子View的宽和高
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
            // 得到LayoutParams
            MarginLayoutParams lp = (MarginLayoutParams) child
                    .getLayoutParams();

            // 子View占据的宽度
            int childWidth = child.getMeasuredWidth() + lp.leftMargin
                    + lp.rightMargin;
            // 子View占据的高度
            int childHeight = child.getMeasuredHeight() + lp.topMargin
                    + lp.bottomMargin;

            // 换行
            if (lineWidth + childWidth > sizeWidth - getPaddingLeft() - getPaddingRight())
            {
                // 对比得到最大的宽度
                width = Math.max(width, lineWidth);
                // 重置lineWidth
                lineWidth = childWidth;
                // 记录行高
                height += lineHeight;
                lineHeight = childHeight;
            } else
            // 未换行
            {
                // 叠加行宽
                lineWidth += childWidth;
                // 得到当前行最大的高度
                lineHeight = Math.max(lineHeight, childHeight);
            }
            // 最后一个控件
            if (i == cCount - 1)
            {
                width = Math.max(lineWidth, width);
                height += lineHeight;
            }
        }

        Log.e("TAG", "sizeWidth = " + sizeWidth);
        Log.e("TAG", "sizeHeight = " + sizeHeight);

        setMeasuredDimension(
                //
                modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width + getPaddingLeft() + getPaddingRight(),
                modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height + getPaddingTop()+ getPaddingBottom()//
        );

    }

    /**
     * 存储所有的View
     */
    private List<List<View>> mAllViews = new ArrayList<List<View>>();
    /**
     * 每一行的高度
     */
    private List<Integer> mLineHeight = new ArrayList<Integer>();

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b)
    {
        mAllViews.clear();
        mLineHeight.clear();

        // 当前ViewGroup的宽度
        int width = getWidth();

        int lineWidth = 0;
        int lineHeight = 0;

        List<View> lineViews = new ArrayList<View>();

        int cCount = getChildCount();

        for (int i = 0; i < cCount; i++)
        {
            View child = getChildAt(i);
            MarginLayoutParams lp = (MarginLayoutParams) child
                    .getLayoutParams();

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

            // 如果需要换行
            if (childWidth + lineWidth + lp.leftMargin + lp.rightMargin > width - getPaddingLeft() - getPaddingRight())
            {
                // 记录LineHeight
                mLineHeight.add(lineHeight);
                // 记录当前行的Views
                mAllViews.add(lineViews);

                // 重置我们的行宽和行高
                lineWidth = 0;
                lineHeight = childHeight + lp.topMargin + lp.bottomMargin;
                // 重置我们的View集合
                lineViews = new ArrayList<View>();
            }
            lineWidth += childWidth + lp.leftMargin + lp.rightMargin;
            lineHeight = Math.max(lineHeight, childHeight + lp.topMargin
                    + lp.bottomMargin);
            lineViews.add(child);

        }// for end
        // 处理最后一行
        mLineHeight.add(lineHeight);
        mAllViews.add(lineViews);

        // 设置子View的位置

        int left = getPaddingLeft();
        int top = getPaddingTop();

        // 行数
        int lineNum = mAllViews.size();

        for (int i = 0; i < lineNum; i++)
        {
            // 当前行的所有的View
            lineViews = mAllViews.get(i);
            lineHeight = mLineHeight.get(i);

            for (int j = 0; j < lineViews.size(); j++)
            {
                View child = lineViews.get(j);
                // 判断child的状态
                if (child.getVisibility() == View.GONE)
                {
                    continue;
                }

                MarginLayoutParams lp = (MarginLayoutParams) child
                        .getLayoutParams();

                int lc = left + lp.leftMargin;
                int tc = top + lp.topMargin;
                int rc = lc + child.getMeasuredWidth();
                int bc = tc + child.getMeasuredHeight();

                // 为子View进行布局
                child.layout(lc, tc, rc, bc);

                left += child.getMeasuredWidth() + lp.leftMargin
                        + lp.rightMargin;
            }
            left = getPaddingLeft() ;
            top += lineHeight ;
        }

    }

    /**
     * 与当前ViewGroup对应的LayoutParams
     */
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs)
    {
        return new MarginLayoutParams(getContext(), attrs);
    }
}
  • 主布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.coder.myapplication.FlowLayout
        android:id="@+id/id_flowlayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="5"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="6"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="7"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="8"/>


    </com.example.coder.myapplication.FlowLayout>

</RelativeLayout>
  • 要添加的子项布局
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:background="#000000"
    android:textColor="#5BC4ED"
    android:text="Helloworld" >

</TextView>
  • acticity
package com.example.coder.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TextView;

public class FlowLayoutActivity extends Activity
{
    private String[] mVals = new String[]
            { "Hello", "Android", "Weclome Hi ", "Button", "TextView", "Hello",
                    "Android", "Weclome", "Button ImageView", "TextView", "Helloworld",
                    "Android", "Weclome Hello", "Button Text", "TextView" };

    private FlowLayout mFlowLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.flowlayout);

        mFlowLayout = (FlowLayout) findViewById(R.id.id_flowlayout);

        initData();
    }

    public void initData()
    {
        LayoutInflater mInflater = LayoutInflater.from(this);
        for (int i = 0; i < mVals.length; i++)
        {
            TextView tv = (TextView) mInflater.inflate(R.layout.tv,
                    mFlowLayout, false);
            tv.setText(mVals[i]);
            mFlowLayout.addView(tv);
        }

    }

}
Android中常见的热门标签的流式布局的实现 一、概述: 在日常的app使用中,我们会在android 的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧(源码下载在下面最后给出) 类似的自定义布局。下面我们就来详细介绍流式布局的应用特点以及用的的技术点: 1.流式布局的特点以及应用场景 特点:当上面一行的空间不够容纳新的TextView时候, 才开辟下一行的空间   原理图: 阅读详情

相关推荐

Android自定义View之实现流式布局

流式布局 把子控件从左到右摆放,如果一行放不下,自动放到下一行 自定义布局流程 1. 自定义属性:声明,设置,解析获取自定义值 在attr.xml中声明 3. 测量:在onMeasure 方法测量自身的宽高和child的宽高 4. 布局:在onLayout方法里面根据自己的规则来确定children的位置 5. 绘制:onDraw 6. 处理layoutParams 7. 触摸反馈:滑动事件 ...

fakerXuan的博客 372

Android 流式布局实现方式

Android 流失布局实现方式

唐诺 1744

【Android】掌握自定义LayoutManager(二) 实现流式布局

在当下这个信息共享的时代,很多资源都可以在网络上找到,只取决于你愿不愿意找或是找的方法对不对了很多朋友不是没有资料,大多都是有几十上百个G,但是杂乱无章,不知道怎么看从哪看起,甚至是看后就忘如果大家觉得自己在网上找的资料非常杂乱、不成体系的话,我也分享一套给大家,比较系统,我平常自己也会经常研读。

2301_79985012的博客 1081

Android 自定义View --- 流式布局FlowLayout

测量子View的方法是View.measure(int widthMeasureSpec, int heightMeasureSpec),widthMeasureSpec和heightMeasureSpec参数可以通过getChildMeasureSpec(int spec, int padding, int childDimension)方法获取,spec是View的MeasureSpec,可以是width或height;//全部子View布局完后,清除子View的保存。//更新这一行已使用的最大高度。

a18211246446的博客 2015

Android 流式布局实现方案全解析

在日常 Android 开发中,经常会遇到 流式布局 (Flow Layout) 的需求,比如:所谓“流式布局”,就是子 View 按行排列,如果一行放不下,就自动换行,类似于 HTML/CSS 的 flex-wrap: wrap 效果。那么在 Android 中,如何实现流式布局呢?Google 官方开源的 FlexboxLayout 是实现流式布局的首选,它模仿了 CSS 的 Flexbox,支持换行、对齐、弹性伸缩等特性。引入依赖: 对应的仓库地址: 动态添加 ✅ 优点:官方维护,功能完善,属性和

折翅鵬的博客 714

Android流式布局深入实现及扩展教程

本文还有配套的精品资源,点击获取 简介:流式布局在Android开发中用于展示大量元素,并能自动适应不同屏幕尺寸。本教程将详细介绍流式布局的基本概念、实现原理、关键方法如onMeasure和onLayout的重写,以及如何实现子项的添加和移除。同时,还将探讨功能扩展,包括不同的对齐方式、间距边距设置、最大行数限制、自适应布局调整、点击事件监听和动画效果实现。通过本教程,开发...

weixin_33072399的博客 1456

Android 流式布局

       今天我们来看Android的流式布局。 所谓流式布局指的是ViewGroup中同一行的宽度不足以容纳下一个子view时,进行换行处理,而不需要考虑子view的大小,每一行的高度以其中最高者为准。自定义ViewGroup的三种方式:1.通过继承现有的ViewGroup(比如LinearLayout)重写 onMeasure() 来修改已有的 ViewGroup的尺寸,比如你需要一个正方...

qq_41789379的博客 421

计算机考研408复试题汇总

TCP连接是全双工的,两端都可以先挥手 其中一端完成数据发送任务后,发送一个FIN包来终止,另一端针对这个FIN包回复确认包, 直到另一端也发送请求终止数据包FIN,另一端作出确认应答,并且还要发送一个ACK=1表示确认号有效,如果没有收到这个就可以进行重传.

ca1m4n的博客 1万+

Android流式布局实现

Android流式布局实现 通过辛勤工作获得财富才是人生的大快事。 如何通过代码实现类似于一个一个的textview控件放入父控件中,如果一行放不下自动换行呢?今天我们就来实现这个效果,本文的目的旨在了解其原理,如果需要应用到时间项目中还需完善,废话不多说上图! 1.创建一个类集成子ViewGroup 这里重写下列几个方法,FlowLayout(Context context, AttributeSet attrs)用于再布局文件中使用;LayoutParams generateDefaultLayout

qq_36050563的博客 2164

相控阵多功能雷达工作模式识别技术综述

本文综述了相控阵雷达工作模式识别技术的研究进展。首先介绍了相控阵雷达的典型工作模式分类及其信号特征参数,包括搜索模式(VS、RWS、TWS)、跟踪模式(STT、MTT、TAS)和其他模式(SAM、SAR)等。随后重点阐述了基于信号特征的识别方法,包括时域特征(PRI分析、脉冲幅度包络、数据率)、频域特征(载频变化、调制方式)和混合特征分析方法。最后讨论了当前技术面临的挑战与发展趋势,包括复杂信号环境下的识别难题、新型雷达技术的适应需求以及深度学习技术的应用前景。该领域的研究对电子侦察和威胁评估具有重要意义。

Grow into a more wonderful you 1795

JavaFx之集成FontAwesomeFX与JFoenix(十五)

本文详细介绍了如何在JavaFx项目中同时集成JFoenix和FontAwesomeFX,以创建现代化、美观的桌面应用界面。通过Maven依赖配置、动态代码创建和FXML静态声明两种方式,演示了如何将JFoenix的Material Design控件与FontAwesomeFX的丰富矢量图标完美结合,并提供了实战案例展示如何构建一个功能完善的现代化工具栏。

pepsi的博客 437

android分组流式布局,Android 流式布局实现

概述本文主要分享Android流式布局实现,实现效果如下:在实现之前先来看一下View的生命周期,如下图:流式布局属于自定义ViewGroup,重点关注onMeasure与onLayout方法onMeasure完成子控件以及自身宽高测量onMeasure方法中的主要工作:确定子控件的widthMeasureSpec与heightMeasureSpec(重点)根据childWidthMeasureS...

weixin_30486919的博客 485

硬件压力测试工具使用方法

B, --bind host# 绑定到主机的多个地址中的一个。当客户端到服务器端的测试结束时,服务器端通过-l选项指定的端口(或默认为客户端连接到服务器端的端口),反向连接至客户端。-c, --client host#如果Iperf运行在服务器模式,并且用-c参数指定一个主机,那么Iperf将只接受指定主机的连接。-C, --compatibility# 与低版本的Iperf使用时,可以使用兼容模式。-b, --bandwidth #[KM] UDP模式使用的带宽,单位bits/sec。

weixin_53303772的博客 8214

android 横向流式布局,Android流式布局实现

订阅新项目用到了一种全新布局————Android标签流式布局的功能,正好一直说给大家讲自定义控件的实现,今天就为大家讲一种android流式布局的实现。对本文有任何问题,可加我的个人微信询问:kymjs123在日常的app使用中,我们会在android 的app中看见热门标签等自动换行的流式布局,今天,我们就来看看如何自定义一个类似热门标签那样的流式布局吧(源码下载在下面最后给出)这个控件并不是...

weixin_36465073的博客 688

【免费】最新区块链钱包和私钥的助记词碰撞器,使用python开发

最新钱包私钥助记词碰撞器,搏一搏单车变摩托!

weixin_48529185的博客 1万+

Python全栈入门到实战【基础篇 19】函数进阶:默认参数、递归函数与偏函数应用

本文深入探讨Python函数进阶的三大核心知识:默认参数、递归函数与偏函数。针对默认参数,揭示了可变对象作为默认值的陷阱,提出使用None替代的方案;解析递归函数的三大要素(终止条件、递归调用、问题拆解),并通过阶乘和斐波那契数列示例演示实现方法,同时给出尾递归和缓存装饰器两种优化策略;最后介绍偏函数如何通过functools.partial固化参数简化调用。这些技巧能显著提升函数在复杂场景下的灵活性与复用性,帮助开发者编写更高效的Python代码。

那些看似不起波澜的日复一日,会在某天让你看到坚持的意义 3863

android实现流式布局

文章目的:实现流式布局的效果,明白view的绘制流程 运行软件和环境: android Studio 3.5.1 jdk1.8 效果图 自定义viewgroup 自定义viewgroup对子view进行测量和布局 代码 /** * author : Luuuzi * e-mail : wang1143303@163.com * date : 2020/2/26 0026 11:10...

Luuuzi的博客 320

世界职业院校技能大赛财经赛道项目参考逐字稿五

世界职业院校技能大赛财经赛道项目参考逐字稿五

我是网络安全兼技能大赛工程师,专注网络安全技术学习与技能大赛实战!持续分享最新的技术文章,帮你少走弯路,一起在技术赛道上进步~ 129

Android电商 仿京东 流式布局 实现

一、概述: 在日常的app使用中,我们会在Android的app中看见 热门标签等自动换行的流式布局,今天,我们就来看看如何 自定义一个类似热门标签那样的流式布局吧(源码下载在下面最后给出) 类似的自定义布局。下面我们就来详细介绍流式布局的应用特点以及用的技术点: public class MainActivity extends AppCompatActivity { /...

qq_41917499的博客 2043

第1章:认识OpenClaw

OpenClaw是一个开源的 AI 智能体 Gateway 网关项目名称演变:OpenClaw 原名 Clawdbot,因 Anthropic 商标顾虑在 2026年1月27日更名为 Moltbot(过渡名),最终在1月30日确定为 OpenClaw。三个名字本质是同一个项目,功能完全一致。🏠在你的电脑上运行:完全本地部署,数据隐私有保障📁访问本地文件:可以搜索、读取、编辑你电脑上的文件🔌扩展能力:通过Skills(技能)系统无限扩展功能💬多平台使用。

Maynor的博客 2832

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

android 流式布局简单实现

1.概述 今天看了Android自定义控件的书籍之后准备动手写个自定义控件,于是有了该篇文章。之前也看过一些大神们写的,受益匪浅。初写博客一方面是想向大神们学习,另一方面也算是自己学习的一个记录。 2.思路分析 好了废话不多扯,开始正题:流式布局肯定一个view容器也就是ViewGroup。从View的工作原理可以得知 View的绘制流程: onMeasure —–> onLayout —

chlw的博客 966

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

上一篇: Git常用操作
下一篇: SpringBOOT入门
aNoobCoder
博客等级 码龄10年 2粉丝 52原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值