Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android上填充FlowLayout的空白空间_Java_Android_Flowlayout_Fill Parent - Fatal编程技术网

Java 在Android上填充FlowLayout的空白空间

Java 在Android上填充FlowLayout的空白空间,java,android,flowlayout,fill-parent,Java,Android,Flowlayout,Fill Parent,我正在创建一个具有流布局的应用程序。我从web上找到的FlowLayout实现(我不记得确切的位置)。现在,当我的布局宽度大于其子项宽度之和时,我需要填充空白空间,为每个子项提供适当的宽度。我试着给他们MATCH\u PARENT作为宽度,但是没有用,每个元素都是从“新行”创建的请帮我解决我的问题。提前谢谢 下面是我使用的FlowLayout类实现: public class FlowLayout extends ViewGroup { public static final String TA

我正在创建一个具有流布局的应用程序。我从web上找到的
FlowLayout
实现(我不记得确切的位置)。现在,当我的布局宽度大于其子项宽度之和时,我需要填充空白空间,为每个子项提供适当的宽度。我试着给他们
MATCH\u PARENT
作为宽度,但是没有用,每个元素都是从“新行”创建的
请帮我解决我的问题。提前谢谢

下面是我使用的FlowLayout类实现:

public class FlowLayout extends ViewGroup {
public static final String TAG = FlowLayout.class.getSimpleName();

private int mHorizontalSpacing = 10;
private int mVerticalSpacing = 10;

public FlowLayout(Context context) {
    super(context);
}

public FlowLayout(Context context, AttributeSet attrs) {
    super(context, attrs);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FlowLayout);
    try {
        mHorizontalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_horizontalSpacing, 0);
        mVerticalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_verticalSpacing, 0);
    } finally {
        a.recycle();
    }
}

@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);

    int width = 0;
    int height = getPaddingTop();

    int currentWidth = getPaddingLeft();
    int currentHeight = 0;

    boolean breakLine = false;

    final int count = getChildCount();

    Log.d(TAG, "child count = " + count);

    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        measureChild(child, widthMeasureSpec, heightMeasureSpec);

        Log.d(TAG, "child " + i + " mw=" + child.getMeasuredWidth() + " mh=" + child.getMeasuredHeight());

        if (breakLine || currentWidth + child.getMeasuredWidth() > widthSize) {
            height += currentHeight + mVerticalSpacing;
            currentHeight = 0;
            if (currentWidth > width) width = currentWidth;
            currentWidth = getPaddingLeft();
        }

        int spacing = mHorizontalSpacing;
        if (lp.spacing != Integer.MAX_VALUE) {
            spacing = lp.spacing;
        }

        lp.x = currentWidth;
        lp.y = height;

        Log.d(TAG, "child " + i + " x=" + lp.x + " y=" + lp.y);

        currentWidth += child.getMeasuredWidth() + spacing;
        int childHeight = child.getMeasuredHeight();
        if (childHeight > currentHeight) currentHeight = childHeight;

        breakLine = lp.breakLine;
    }

    // after last row (patched by yuku)
    {
        height += currentHeight;
        if (currentWidth > width) width = currentWidth;
    }

    width += getPaddingRight();
    height += getPaddingBottom();

    Log.d(TAG, "onMeasure w=" + width + " h=" + height + " widthMeasureSpec=" + Integer.toHexString(widthMeasureSpec) + " heightMeasureSpec=" + Integer.toHexString(heightMeasureSpec));

    // don't resolve height (patched by yuku)
    setMeasuredDimension(resolveSize(width, widthMeasureSpec), resolveSize(height, heightMeasureSpec));
}

@Override protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        View child = getChildAt(i);
        LayoutParams lp = (LayoutParams) child.getLayoutParams();
        child.layout(lp.x, lp.y, lp.x + child.getMeasuredWidth(), lp.y + child.getMeasuredHeight());
    }
}

@Override protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
    return p instanceof LayoutParams;
}

@Override public LayoutParams generateDefaultLayoutParams() {
    return new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}

@Override public LayoutParams generateLayoutParams(AttributeSet attrs) {
    return new LayoutParams(getContext(), attrs);
}

@Override public LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
    return new LayoutParams(p.width, p.height);
}

public static class LayoutParams extends ViewGroup.LayoutParams {
    public boolean breakLine;
    public int spacing = Integer.MAX_VALUE;

    private int x;
    private int y;

    public LayoutParams(int width, int height) {
        super(width, height);
    }

    public LayoutParams(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FlowLayout_LayoutParams);
        try {
            spacing = a.getDimensionPixelSize(R.styleable.FlowLayout_LayoutParams_layout_spacing, Integer.MAX_VALUE);
            breakLine = a.getBoolean(R.styleable.FlowLayout_LayoutParams_layout_breakLine, false);
        } finally {
            a.recycle();
        }       
    }
}
}
公共类FlowLayout扩展了视图组{
公共静态最终字符串标记=FlowLayout.class.getSimpleName();
私人国际水平间距=10;
专用整数mVerticalSpacing=10;
公共流程布局(上下文){
超级(上下文);
}
公共流布局(上下文、属性集属性){
超级(上下文,attrs);
TypedArray a=context.actainStyledAttributes(attrs,R.styleable.FlowLayout);
试一试{
mHorizontalSpacing=a.getDimensionPixelSize(R.styleable.FlowLayout_horizontalSpacing,0);
mVerticalSpacing=a.getDimensionPixelSize(R.styleable.FlowLayout\u verticalSpacing,0);
}最后{
a、 回收();
}
}
@在测量时覆盖受保护的空隙(int widthMeasureSpec、int heightMeasureSpec){
int widthSize=MeasureSpec.getSize(widthMeasureSpec);
整数宽度=0;
int height=getPaddingTop();
int currentWidth=getPaddingLeft();
int currentHeight=0;
布尔特征线=假;
最终整数计数=getChildCount();
Log.d(标签,“子计数=”+计数);
for(int i=0;iwidthSize){
高度+=当前高度+垂直间距;
当前高度=0;
如果(当前宽度>宽度)宽度=当前宽度;
currentWidth=getPaddingLeft();
}
int间距=mh水平间距;
if(lp.spating!=整数最大值){
间距=lp.间距;
}
lp.x=电流宽度;
lp.y=高度;
Log.d(标记“child”+i+“x=“+lp.x+”y=“+lp.y”);
currentWidth+=child.getMeasuredWidth()+间距;
int childHeight=child.getMeasuredHeight();
如果(childHeight>currentHeight)currentHeight=childHeight;
特征线=lp.特征线;
}
//最后一行之后(由yuku修补)
{
高度+=当前高度;
如果(当前宽度>宽度)宽度=当前宽度;
}
宽度+=getPaddingRight();
高度+=getPaddingBottom();
Log.d(标记,“onMeasure w=“+width+”h=“+height+”widthmasurespec=“+Integer.tohextstring(widthmasurespec)+“heightMeasureSpec=“+Integer.tohextstring(heightMeasureSpec));
//不解析高度(由yuku修补)
设置测量尺寸(resolveSize(宽度,宽度测量等级),resolveSize(高度,高度测量等级));
}
@覆盖受保护的仅空布局(布尔值已更改、int l、int t、int r、int b){
最终整数计数=getChildCount();
for(int i=0;i
我想达到这个效果:

与此相反:


我想我明白了,这有点棘手,但这里或多或少有一些你需要做的事情。在最初的for循环中,跟踪是否已转到新的行。假设您调用此变量
isOneLine
,并将其初始化为true,一旦将currentWidth重置为0,则将其设置为false。然后,在for循环之外:

if (isOneLine) {
  //There's always N + 1 padding "spaces"
  int paddingBetween = (widthSize - currentWidth)/(count + 1); 

  for (int i = 0; i < count; i++) {
    View child = getChildAt(i);
    LayoutParams lp = (LayoutParams) child.getLayoutParams();
    measureChild(child, widthMeasureSpec, heightMeasureSpec);

    int spacing = mHorizontalSpacing;
    if (lp.spacing != Integer.MAX_VALUE) {
        spacing = lp.spacing;
    }
    //Not sure exactly what spacing is here, but I assume you have to take it into account somewhere...
    lp.x = paddingBetween + currentWidth;
    lp.y = 0;

    currentWidth += child.getMeasuredWidth() + spacing + paddingBetween;
  }
}
if(单线){
//总有N+1填充“空格”
int paddingBetween=(宽度大小-当前宽度)/(计数+1);
for(int i=0;i