2013-04-25 17 views
0

我有一個GridView,我已經用64 60x60px png填充了。我希望GridView儘可能接近完美的正方形顯示它們,所以我已經將XML中的numColumns設置爲8,所以現在我有一個8x8的網格。如何停止GridView剪切我的圖像?

這裏是什麼樣子:

enter image description here

我的圖片實際上是在非常邊緣有一個小的邊界,雖然是被裁剪掉。在這裏,我畫左上角的圖像上,他們應該是什麼顯示時,如:

enter image description here

這裏是我的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; 
} 
+0

你試過設置ScaleType在的GridItem的ImageView的由於Android:scaleType = 「fitCenter」。 – SKK 2013-04-25 11:09:41

+0

我不確定這個問題,但也許會出現您的問題,因爲您的固定列數已定義。你的網格沒有空間顯示全部8列,所以它裁剪。你能否在numColumns上嘗試:'auto_fit'而不是'8'? – Bram 2013-04-25 11:13:37

+0

@Bram我會試着看看它是否消除了裁剪,但我不能那樣做,因爲我必須保持網格。 – ChrisWilson4 2013-04-25 11:20:57

回答

0

要使用flowlayout使一個名爲FlowLayout的java類成爲android中的自定義控件。

.../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 /價值/ 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"將把該項目放在一個新行。

這是從這個混帳編輯:https://github.com/ApmeM/android-flowlayout

+0

感謝您向我展示此替代方案。 – ChrisWilson4 2013-04-25 22:16:28

+0

沒問題,我個人不喜歡android中的gridview。 – 2013-04-26 06:13:19

+0

這種感覺正逐漸成爲互相影響的時刻。 – ChrisWilson4 2013-04-26 07:07:48