Android-将项目像标签一样堆叠在一起
我所拥有的: 我想做的是: 每当我添加项目时,我希望它放在前面的项目旁边。如果某个元素位于屏幕的末尾:我希望将其放置在下一行。Android-将项目像标签一样堆叠在一起,android,Android,我所拥有的: 我想做的是: 每当我添加项目时,我希望它放在前面的项目旁边。如果某个元素位于屏幕的末尾:我希望将其放置在下一行。 类似于float:left在CSS中,如果希望标签容器最终滚动,那么使用并调用它setPanCount(3)。不要忘记将其方向设置为VERTICAL 如果您不需要滚动您的项目,那么只需使用 编辑: 我已经列出了一个粗略的例子,它按照您想要的方式工作。您可能还需要考虑边距和填充,但它应该作为一个基本的想法来为您服务 public class TagLayout ext
类似于
float:left
在CSS中,如果希望标签容器最终滚动,那么使用并调用它setPanCount(3)
。不要忘记将其方向设置为VERTICAL
如果您不需要滚动您的项目,那么只需使用
编辑:
我已经列出了一个粗略的例子,它按照您想要的方式工作。您可能还需要考虑边距和填充,但它应该作为一个基本的想法来为您服务
public class TagLayout extends ViewGroup {
public TagLayout(Context context) {
super(context);
}
public TagLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TagLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public TagLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
int maxWidth = getMeasuredWidth();
int count = getChildCount();
int tagLeft = left,
tagTop = top,
tagRight = 0,
tagBottom = 0;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
tagRight = tagLeft + child.getMeasuredWidth();
tagBottom = tagTop + child.getMeasuredHeight();
if (tagRight > maxWidth) {
tagLeft = left;
tagRight = left + child.getMeasuredWidth();
tagTop = tagBottom;
tagBottom += child.getMeasuredHeight();
}
child.layout(tagLeft, tagTop, tagRight, tagBottom);
tagLeft = left;
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int width = 0;
int height = 0;
int count = getChildCount();
int maxWidth = MeasureSpec.getSize(widthMeasureSpec);
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (child.getVisibility() != GONE) {
measureChild(child, widthMeasureSpec, heightMeasureSpec);
width += child.getMeasuredWidth();
if (height == 0) {
height += child.getMeasuredHeight();
}
if (width <= maxWidth) {
continue;
} else {
width = 0;
height += child.getMeasuredHeight();
}
}
}
setMeasuredDimension(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
}
}
public类标记布局扩展了视图组{
公共标记布局(上下文){
超级(上下文);
}
公共标记布局(上下文、属性集属性){
超级(上下文,attrs);
}
公共标记布局(上下文上下文、属性集属性、int-defStyleAttr){
super(上下文、attrs、defStyleAttr);
}
公共标记布局(上下文上下文、属性集属性、int-defStyleAttr、int-defStyleRes){
super(context、attrs、defStyleAttr、defStyleRes);
}
@凌驾
仅限受保护的空心布局(布尔值已更改、整数左侧、整数顶部、整数右侧、整数底部){
int maxWidth=getMeasuredWidth();
int count=getChildCount();
int tagLeft=left,
tagTop=top,
tagRight=0,
tagBottom=0;
for(int i=0;imaxWidth){
tagLeft=左;
tagRight=left+child.getMeasuredWidth();
tagTop=tagBottom;
tagBottom+=child.getMeasuredHeight();
}
子布局(标记左、标记上、标记右、标记下);
tagLeft=左;
}
}
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
整数宽度=0;
整数高度=0;
int count=getChildCount();
int maxWidth=MeasureSpec.getSize(widthMeasureSpec);
for(int i=0;i if(width您需要的是一个类似Java Swing FlowLayout的布局。您在web上有一些它的实现。这个库看起来很有希望,但我自己还没有尝试过
如果这对您不起作用,我将编辑并发布我使用的FlowLayout实现。我现在不能这样做,因为我离开了我的工作机器。请查看编辑以了解为什么不可能。您描述的内容通常被称为FlowLayout。已经有一些可以帮助您。这正是我要找的,谢谢!请请参见编辑。它实际上不是网格,它就像浮点:在cssOh中,我想到的第一件事是使用StaggedGridLayoutManager
,但我想它实际上不起作用。然后我会构建一个自定义视图组,但这个视图组要复杂得多,您必须重写并实现onLayout
和onMeasure
方法与您的自定义逻辑。很抱歉,我现在没有时间。例如,可能有更有经验的人可以编写它,而无需花费大量时间进行深入研究。我最终使用了FlowLayout库。不过,感谢您的努力。