ScrollView使内部GridLayout消失

时间:2018-09-12 20:15:51

标签: android scrollview android-gridlayout

我的目标:建立某种可滚动的画廊(以作更改)。我在CardView中使用了GridLayout。

我已经阅读了很多线程,发现需要插入ScrollView并将要滚动的内容放入其中。问题是:当我在周围添加ScrollView时,为什么要滚动的内容消失了?

在没有ScrollView的情况下,它可以完美显示(除了,因此,所有内容都显示在同一“页面”上,因此太小了)。

您能解释一下为什么它不起作用吗?

非常感谢:)

这是我的xml代码:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".RecipeActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="8"
            android:alignmentMode="alignMargins"
            android:columnOrderPreserved="false"
            android:rowCount="3"
            android:columnCount="2">

            <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    android:focusable="true"
                    android:clickable="true"
                    android:onClick="seeRecipe">

                    <ImageView
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:src="@drawable/chocolatine"/>

                    <!-- random TextView -->

                </LinearLayout>

            </android.support.v7.widget.CardView>

            <!-- 15 more android.support.v7.widget.CardView -->

        </GridLayout>

    </ScrollView>

</LinearLayout>

我的Java代码为空,除了必要的内容(onCreate ...)。

1 个答案:

答案 0 :(得分:0)

将事情修剪得很细,我看到了:

<GridLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="8"

此GridLayout的父标记是<ScrollView>,并且ScrollViews 支持布局权重的概念。这意味着您只需将GridLayout的高度定义为零,这就是为什么它不再显示的原因。

在我看来,您可以通过删除layout_weight属性并将高度设置为wrap_content来解决此问题。

相关问题