0

我有一个填充了 64 个 60x60px png 的 GridView。我希望 GridView 将它们全部显示为尽可能接近完美的正方形,因此我将 XML 中的 numColumns 设置为 8,所以现在我有一个 8x8 网格。

这是它的样子:

在此处输入图像描述

我的图像实际上在边缘有一个小边框,尽管它被裁剪掉了。在这里,我在左上角的图像上绘制了它们显示时的样子:

在此处输入图像描述

这是我的 XML:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textFieldFU"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <GridView
    android:id="@+id/gridview"
    android:layout_width="fill_parent" 
    android:layout_height="600dp"
    android:numColumns="8"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth"
    android:gravity="center"
/>

</RelativeLayout>

当我使用 40x40px 和 50x50px 大小的 png 时,它们工作得很好,但它们太小而无法轻易看到我的小符号。我已经更改了 XML 中我能想到的所有内容,但是无论我给多少间距或在哪里给它,即使有足够的空间,图像也会保持裁剪。

如何让 GridView 显示完整的、未裁剪的图像?

为了爱所有神圣的事物,我真的很笨。我忘记了在我设置的“ImageAdapter”类中使用了 ImageView setLayoutParams 方法并将它们设置为 (50, 50)。很抱歉浪费了你们好人的时间。

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView iv;
    if (convertView != null) {
        iv = (ImageView) convertView;
    } else {
        iv = new ImageView(context);
        ******iv.setLayoutParams(new GridView.LayoutParams(50, 50));******
        iv.setScaleType(ScaleType.CENTER);
        iv.setPadding(0, 0, 0, 0);
    }
    iv.setImageResource(images[position]);
    return iv;
}
4

1 回答 1

0

要使用 flowlayout FlowLayout,请在 android 中创建一个称为自定义控件的 java 类。

.../src/FlowLayout.java:

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

public class FlowLayout extends ViewGroup
{
    public static final int HORIZONTAL = 0;
    public static final int VERTICAL = 1;

    private int horizontalSpacing = 20;
    private int verticalSpacing = 20;
    private int orientation = 0;

    private int innerPadding = 12;

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

    public FlowLayout(Context context, AttributeSet attributeSet)
    {
        super(context, attributeSet);
    }

    public FlowLayout(Context context, AttributeSet attributeSet, int defStyle)
    {
        super(context, attributeSet, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
    {
        int sizeWidth = MeasureSpec.getSize(widthMeasureSpec) - this.getPaddingRight() - this.getPaddingLeft();
        int sizeHeight = MeasureSpec.getSize(heightMeasureSpec) - this.getPaddingRight() - this.getPaddingLeft()+innerPadding;

        int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
        int modeHeight = MeasureSpec.getMode(heightMeasureSpec);

        int size;
        int mode;

        if (orientation == HORIZONTAL)
        {
            size = sizeWidth;
            mode = modeWidth;
        }
        else
        {
            size = sizeHeight;
            mode = modeHeight;
        }

        int lineThicknessWithSpacing = 0;
        int lineThickness = 0;
        int lineLengthWithSpacing = 0;
        int lineLength;

        int prevLinePosition = 0;

        int controlMaxLength = 0;
        int controlMaxThickness = 0;

        final int count = getChildCount();
        for (int i = 0; i < count; i++)
        {
            final View child = getChildAt(i);
            if (child.getVisibility() == GONE)
                continue;

            child.measure
            (
                MeasureSpec.makeMeasureSpec(sizeWidth, modeWidth == MeasureSpec.EXACTLY ? MeasureSpec.AT_MOST : modeWidth),
                MeasureSpec.makeMeasureSpec(sizeHeight, modeHeight == MeasureSpec.EXACTLY ? MeasureSpec.AT_MOST : modeHeight)
            );

            LayoutParams lp = (LayoutParams) child.getLayoutParams();

            int hSpacing = this.getHorizontalSpacing(lp);
            int vSpacing = this.getVerticalSpacing(lp);

            int childWidth = child.getMeasuredWidth();
            int childHeight = child.getMeasuredHeight();

            int childLength;
            int childThickness;
            int spacingLength;
            int spacingThickness;

            if (orientation == HORIZONTAL)
            {
                childLength = childWidth;
                childThickness = childHeight;
                spacingLength = hSpacing;
                spacingThickness = vSpacing;
            }
            else
            {
                childLength = childHeight;
                childThickness = childWidth;
                spacingLength = vSpacing;
                spacingThickness = hSpacing;
            }

            lineLength = lineLengthWithSpacing + childLength;
            lineLengthWithSpacing = lineLength + spacingLength;

            boolean newLine = lp.newLine || (mode != MeasureSpec.UNSPECIFIED && lineLength > size);
            if (newLine)
            {
                prevLinePosition = prevLinePosition + lineThicknessWithSpacing;

                lineThickness = childThickness;
                lineLength = childLength;
                lineThicknessWithSpacing = childThickness + spacingThickness;
                lineLengthWithSpacing = lineLength + spacingLength;
            }

            lineThicknessWithSpacing = Math.max(lineThicknessWithSpacing, childThickness + spacingThickness);
            lineThickness = Math.max(lineThickness, childThickness);

            int posX;
            int posY;
            if (orientation == HORIZONTAL)
            {
                posX = innerPadding + getPaddingLeft() + lineLength - childLength;
                posY = getPaddingTop() + prevLinePosition;
            }
            else
            {
                posX = getPaddingLeft() + prevLinePosition;
                posY = innerPadding + getPaddingTop() + lineLength - childHeight;
            }
            lp.setPosition(posX, posY);

            controlMaxLength = Math.max(controlMaxLength, lineLength);
            controlMaxThickness = prevLinePosition + lineThickness;
        }

        if (orientation == HORIZONTAL)
            this.setMeasuredDimension(resolveSize(controlMaxLength, widthMeasureSpec), resolveSize(controlMaxThickness, heightMeasureSpec));
        else
            this.setMeasuredDimension(resolveSize(controlMaxThickness, widthMeasureSpec), resolveSize(controlMaxLength, heightMeasureSpec));
    }

    private int getVerticalSpacing(LayoutParams lp)
    {
        int vSpacing;
        if (lp.verticalSpacingSpecified())
            vSpacing = lp.verticalSpacing;
        else
            vSpacing = this.verticalSpacing;
        return vSpacing;
    }

    private int getHorizontalSpacing(LayoutParams lp)
    {
        int hSpacing;
        if (lp.horizontalSpacingSpecified())
            hSpacing = lp.horizontalSpacing;
        else
            hSpacing = this.horizontalSpacing;
        return hSpacing;
    }

    @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 drawChild(Canvas canvas, View child, long drawingTime)
    {
        return super.drawChild(canvas, child, drawingTime);
    }

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

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

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

    @Override
    protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p)
    {
        return new LayoutParams(p);
    }

    public static class LayoutParams extends ViewGroup.LayoutParams
    {
        private static int NO_SPACING = -1;

        private int x;
        private int y;
        private int horizontalSpacing = NO_SPACING;
        private int verticalSpacing = NO_SPACING;
        private boolean newLine = false;

        public LayoutParams(Context context, AttributeSet attributeSet)
        {
            super(context, attributeSet);
            this.readStyleParameters(context, attributeSet);
        }

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

        public LayoutParams(ViewGroup.LayoutParams layoutParams)
        {
            super(layoutParams);
        }

        public boolean horizontalSpacingSpecified()
        {
            return horizontalSpacing != NO_SPACING;
        }

        public boolean verticalSpacingSpecified()
        {
            return verticalSpacing != NO_SPACING;
        }

        public void setPosition(int x, int y)
        {
            this.x = x;
            this.y = y;
        }

        private void readStyleParameters(Context context, AttributeSet attributeSet)
        {
            TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.FlowLayout_LayoutParams);
            try
            {
                horizontalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_LayoutParams_layout_horizontalSpacing, NO_SPACING);
                verticalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_LayoutParams_layout_verticalSpacing, NO_SPACING);
                newLine = a.getBoolean(R.styleable.FlowLayout_LayoutParams_layout_newLine, false);
            }
            finally
            {
                a.recycle();
            }
        }
    }
}

然后,您为将要位于流布局视图中的视图创建自定义属性。

.../res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FlowLayout_LayoutParams">
        <attr name="layout_newLine" format="boolean"/>
        <attr name="layout_horizontalSpacing" format="dimension"/>
        <attr name="layout_verticalSpacing" format="dimension"/>
    </declare-styleable>
</resources>

然后在 xml 布局中添加:

<[PATH_TO_CLASS].FlowLayout
    xmlns:flowLayout="http://schemas.android.com/apk/res/za.co.lawdata.searchworks"
    android:id="@+id/flow_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        flowLayout:layout_verticalSpacing="50dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        flowLayout:layout_newLine="true"
        flowLayout:layout_horizontalSpacing="50dp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"/>

</[PATH_TO_CLASS].FlowLayout>

并替换[PATH_TO_CLASS]为您的包路径,例如:com.example.appname

flowLayout:layout_verticalSpacing="50dp"将设置项目之间的垂直空间。默认设置在 java 类中。

flowLayout:layout_horizontalSpacing="50dp"将设置项目之间的水平空间。默认设置在 java 类中。

flowLayout:layout_newLine="true"会将项目放在新行上。

这是来自这个 git 的编辑:https ://github.com/ApmeM/android-flowlayout

于 2013-04-25T12:34:21.720 回答