2016-09-07 40 views
0

出于某种原因,我的CardView仅在网格视图中显示第一行图像。 (或者我的GridView只显示第一行 - 我看不出来)。CardView仅显示GridLayout的第一行

我动态地将图像添加到gridView中,并使用gridAdapter.notifyDataSetChanged()。也许这就是为什么CardView的高度不适应?当我向CardView添加android:layout_height =“500dp”时,我会看到所有图像。

有没有办法让CardView包装内容,以便我可以看到我的所有图像(除了硬编码高度)?

请注意,由于照片的行数是动态的,我无法对高度进行硬编码。

只显示图像的一行,即使实际上是有2行 enter image description here

<android.support.v7.widget.CardView 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="2dp"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/m_photos" /> 

    <GridView 
     android:id="@+id/m_grid" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="2dp" 
     android:clickable="true" 
     android:columnWidth="90dp" 
     android:drawSelectorOnTop="true" 
     android:focusable="true" 
     android:gravity="center" 
     android:numColumns="auto_fit" 
     android:stretchMode="none" 
     android:verticalSpacing="2dp" /> 
</LinearLayout> 
</android.support.v7.widget.CardView> 

回答

0

我面对这个问题几天ago.For这种情况下,不WRAP_CONTENT工作perfectly.So图像在固定的dp中使用布局高度,并使用该高度。它适用于我的情况。

<GridView 
    android:id="@+id/m_grid" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/gvHeight" 
    android:layout_margin="2dp" 
    android:clickable="true" 
    android:columnWidth="90dp" 
    android:drawSelectorOnTop="true" 
    android:focusable="true" 
    android:gravity="center" 
    android:numColumns="auto_fit" 
    android:stretchMode="none" 
    android:verticalSpacing="2dp" /> 
+0

如果存在多行照片,则此功能起作用。但是,照片的数量是动态的,并且当只有一行照片时,我不想显示多行。 – Vinnie