Android 实现一个简易横向流式布局

SimpleFlowLayout:一个简易的横向流式布局,只实现核心功能,使用者可自行扩展
  Demo图片如下所示:


Image text Image text Image text

SimpleFlowLayout直接继承自ViewGroup,主要负责实现的功能点为:

  1. 自定义属性:提供每行Item间距,行与行之间的间距属性,方便使用者更改
  2. onMeasure()测量方法: 负责测量当宽度和高度为MatchParent和WrapContent以及SimpleFlowLayout被嵌套在ScrollView或者NestedScrollView中时的measure情况,还有使用Padding的情况
  3. onLayout()摆放方法:直接继承自ViewGroup时必须要实现的方法

一、实现自定义属性:
  在Values下新建attrs.xml属性文件,加入自定义属性

<resources>
    <declare-styleable name="SimpleFlowLayout">
        <attr name="hor_divider_width" format="dimension"/>
        <attr name="hor_row_height" format="dimension"/>
    </declare-styleable>
</resources>

  其次在布局文件中使用

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    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="wrap_content"
    tools:context=".MainActivity">

    <!--hor_divider_width:每行的Item间距
        hor_row_height:行间距-->
    <com.wjr.simple_flow_layout.SimpleFlowLayout
        android:id="@+id/flow_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        app:hor_divider_width="15dp"
        app:hor_row_height="15dp"/>

</android.support.v4.widget.NestedScrollView>

  最后在自定义View中获取到自定义属性

public SimpleFlowLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
   
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值