【问题标题】:CardView only showing first row of GridLayoutCardView 只显示 GridLayout 的第一行
【发布时间】:2017-01-15 11:23:57
【问题描述】:

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

我正在将图像动态添加到 gridView 并使用gridAdapter.notifyDataSetChanged()。也许这就是 CardView 的高度不适应的原因?当我将 android:layout_height="500dp" 添加到 CardView 时,我会看到我所有的图像。

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

请注意,我无法硬编码高度,因为照片的行数是动态的。

只显示一排图片,即使实际上有两排图片

<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>

【问题讨论】:

    标签: android android-studio gridview android-cardview cardview


    【解决方案1】:

    几天前我遇到了这个问题。对于这种情况,Wrap_content 无法正常工作。所以在固定 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" />
    

    【讨论】:

    • 这适用于多行照片。但是,照片的数量是动态的,我不想在只有一行照片时显示多行。
    猜你喜欢
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多