2013-05-29 16 views
10

Ich habe alle Arten von Problemen mit Zeilenhöhen in GridView.Setzen Sie Fixed GridView Zeilenhöhe

Ich möchte die Größe der Elemente (Höhe/Breite) in XML festlegen und dann einfach die GridView Autofit so viele von ihnen wie es kann ohne Stretching. Wenn es nicht in das nächste Element passt, sollte es nur um die aktuelle Anzahl von Elementen herum angepasst werden, die es anpassen konnte.

Momentan bekomme ich 2 Spalten (was mir fast feste Größe scheint) und die Reihen werden gestreckt. Könnte jemand bitte helfen, zu erklären, was passiert und wie ich erreichen kann, was ich will?

Gridview:

<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/main_grid" 
    android:numColumns="auto_fit" 
    android:gravity="center" 
    android:paddingRight="20dp" 
    android:paddingLeft="20dp" 
    android:clipToPadding="true" 
    android:fitsSystemWindows="true" 
    android:stretchMode="none" 
    android:background="@drawable/main_grid_background"> 

</GridView> 

GridItem (Ich will es 320x320 wie ich später ein Hintergrundbild in sie einfügen, die, wenn sie nicht ein perfektes Quadrat ungerade aussieht).

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="320dp" 
    android:layout_height="320dp" 
    android:padding="10dp" > 

    <TextView 
     android:id="@+id/grid_item_label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:text="@+id/label" 
     android:layout_marginTop="5dp" 
     android:textColor="@color/black" 
     android:textSize="15sp" 
     android:visibility="invisible" 
     android:layout_centerInParent="true" > 
    </TextView> 

</RelativeLayout> 

Java:

public View getView(int position, View convertView, ViewGroup parent) { 

     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     View gridView; 

     if (convertView == null) { 

      gridView = new View(context); 

      // get layout from mobile.xml 
      gridView = inflater.inflate(R.layout.main_grid_item, null); 

     } else { 
      gridView = (View) convertView; 
     } 
     TextView textView = (TextView) gridView 
       .findViewById(R.id.grid_item_label); 

      //SET TEXT AND BACKGROUND IMAGE HERE 
      //gridView.setBackgroundResource(R.drawable.main_grid_item_import); 



     return gridView; 
    } 
+0

wenn möglich bitte auf den Screenshot von gereckt Reihen –

+0

@MehulJoisar wenden Sie sich an Bilder für Details siehe. – AlexIIP

+0

'android: stretchMode =" spacingWidthUniform "' ist nicht was du wolltest? –

Antwort

1

Also, das ist nicht eine perfekte Antwort, aber das ist, wie ich es zu arbeiten. Ideen geliehen von this Thread.

All dies funktioniert, vorausgesetzt, ich kenne meine Bildgrößen, die 320x320 sind. Auch in GridView musste ich android:columnWidth="320dp" setzen oder es würde nicht funktionieren. Wenn jemand eine bessere Idee hat, bitte posten Sie es ... für jetzt gehe ich weiter.

Grid Artikel XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="10dp" > 
    <ImageView 
    android:id="@+id/grid_item_image" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true"/> 
    <TextView 
     android:id="@+id/grid_item_label" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:text="@+id/label" 
     android:textColor="@color/black" 
     android:layout_alignLeft="@+id/grid_item_image" 
     android:layout_alignTop="@+id/grid_item_image" 
     android:layout_alignRight="@+id/grid_item_image" 
     android:layout_alignBottom="@+id/grid_item_image" 
     android:gravity="center" 
     android:textSize="15sp" 
     android:visibility="invisible"> 
    </TextView> 

</RelativeLayout> 

Gridview XML:

<GridView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/main_grid" 
    android:numColumns="auto_fit" 
    android:gravity="center" 
    android:columnWidth="320dp" 
    android:paddingRight="20dp" 
    android:paddingLeft="20dp" 
    android:clipToPadding="true" 
    android:fitsSystemWindows="true" 
    android:stretchMode="columnWidth" 
    android:background="@drawable/main_grid_background"> 
</GridView> 
42

Im CustomAdapter

v.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, rowHigh)); 

rowHigh die Dimension ist, müssen Sie ändern

+1

Danke dafür. Abhängig von der App möchten Sie dies möglicherweise in v.post (new Runnable ...) aufrufen, wenn Sie v als endgültiges oder gleichwertiges ViewHolder-Objekt als endgültig festlegen. Ich habe eine 'schließen' Schaltfläche in einer Zelle, die sich selbst entfernt (list.remove (position)) und notifyDataSetChanged() aufruft. Einfach nur setLayoutParams reagierte auf mich (Rendering Timing wurde vermasselt) und setzte es in Post, um es zu beheben. –

+1

Insbesondere sollte es in getView() oder newView() gehen, glaube ich. Siehe Beispiel in der Dokumentation: http://developer.android.com/guide/topics/ui/layout/gridview.html#example –

4
public class MyGridView extends GridView { 
    public MyGridView(Context context) { 
     super(context); 
    } 

    public MyGridView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public MyGridView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     int heightSpec; 

     if (getLayoutParams().height == LayoutParams.WRAP_CONTENT) { 
      // The great Android "hackatlon", the love, the magic. 
      // The two leftmost bits in the height measure spec have 
      // a special meaning, hence we can't use them to describe height. 
      heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST); 
     } else { 
      heightSpec = heightMeasureSpec; 
     } 

     super.onMeasure(widthMeasureSpec, heightSpec); 
    } 


} 

In XML:

<MyGridView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/main_grid" 
    android:gravity="center"/> 
+0

Entnommen von http://www.jayway.com/2012/10/04/how-to -make-the-height-von-einem-gridview-wrap-it-content / –

Verwandte Themen