2014-12-08 6 views
6

Hallo Ich entwickle kleine Android-Anwendung, in der ich einfache Gridview mit einigen Elementen anzeigen möchte.Sie funktioniert ordnungsgemäß. Das einzige Problem ist, dass es immer nur zwei Spalten anzeigt, obwohl es Platz gibt. Sein Bildschirm wird gleichmäßig in zwei Spalten geteilt und zeigt nur zwei Elemente an. Wenn ich die Anzahl der Spalten als Zahl einstelle, d. H. Nicht automatisch anpassen, wird sie korrekt angezeigt. Mein Code sieht so aus:Android Gridview android: numColumns = "auto_fit" immer nur zwei Spalten erstellen

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 
<GridView 
    android:id="@+id/gridView" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_margin="5dp" 
    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp"> 
</GridView> 
</FrameLayout> 

und mein Gitterelement wie folgt aussieht:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
tools:context="com.example.androidcardlayout.MainActivity" > 

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/card_view" 
    android:layout_width="100dp" 
    android:layout_height="150dp" 
    card_view:cardCornerRadius="4dp"> 

    <RelativeLayout 
     android:id="@+id/cardMianRlt" 
     android:layout_width="100dp" 
     android:layout_height="150dp" 
     > 
    </RelativeLayout> 

Mache ich etwas falsch? Hilfe benötigen. Vielen Dank.

Antwort

9

Es sieht so aus, als ob die Einstellung für die automatische Anpassung nur gilt, wenn Sie feste Spaltenbreiten haben. Hier ist der einzige Ort in Gridview-Source-Code, der die Auto-fit Einstellung verwendet:

private boolean determineColumns(int availableSpace) { 
    final int requestedHorizontalSpacing = mRequestedHorizontalSpacing; 
    final int stretchMode = mStretchMode; 
    final int requestedColumnWidth = mRequestedColumnWidth; 
    boolean didNotInitiallyFit = false; 

    if (mRequestedNumColumns == AUTO_FIT) { 
     if (requestedColumnWidth > 0) { 
      // Client told us to pick the number of columns 
      mNumColumns = (availableSpace + requestedHorizontalSpacing)/
        (requestedColumnWidth + requestedHorizontalSpacing); 
     } else { 
      // Just make up a number if we don't have enough info 
      mNumColumns = 2; 
     } 
    } 

Diese private Funktion wird während der Maßnahme/Layout-Prozesses genannt. Beachten Sie, dass innerhalb der Auto-Fit-If-Anweisung, es sei denn requestedColumnWidth > 0, dann erhalten Sie nur 2 Spalten, was Sie sehen.

Wenn eine feste Breite für Ihre App funktioniert, dann müssen Sie es in Ihrem XML so setzen:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    > 
    <GridView 
     android:id="@+id/gridView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_margin="5dp" 
     android:numColumns="auto_fit" 
     android:columnWidth="30dp" 
     android:verticalSpacing="10dp" 
     android:horizontalSpacing="10dp"> 
    </GridView> 
</FrameLayout> 
+0

Ok den Punkt und seine Arbeits bekam. Vielen Dank. – nilkash

+0

können Sie teilen, wie Sie einstellen? – Erum