【View】FlowLayout

文章介绍了如何在Android中使用FlowLayout,这是一个将子视图水平布局直到一行填满然后换行的组件。源码来自GoogleMaterialDesign库,但需要单独复制使用。它提供了设置子视图间距和行间距的功能,并支持单行或多行布局模式。
drawing

FlowLayout 使用场景

热门标签,历史搜索记录

官方的 FlowLayout

位于 com.google.android.material.internal 包下的 FlowLayout 无法直接使用。需要单独复制一份代码出来。

//设置item距离
app:itemSpacing="8dp"
//设置行间距
app:lineSpacing="8dp"

源码

基于 implementation 'com.google.android.material:material:1.7.0'

/*
 * Copyright 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.block.androidui.widget;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.core.view.MarginLayoutParamsCompat;
import androidx.core.view.ViewCompat;

import com.google.android.material.R;

/**
 * Horizontally lay out children until the row is filled and then moved to the next line. Call
 * {@link FlowLayout#setSingleLine(boolean)} to disable reflow and lay all children out in one line.
 *
 * @hide
 */
public class FlowLayout extends ViewGroup {
   
   
  private int lineSpacing;
  private int itemSpacing;
  private boolean singleLine;
  private int rowCount;

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

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

  public FlowLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
   
   
    super(context, attrs, defStyleAttr);
    singleLine = false;
    loadFromAttributes(context, attrs)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值