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)

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

被折叠的 条评论
为什么被折叠?



