Android,GridLayout 的水平和垂直滚动

7

我在水平滚动 GridLayout 上遇到了麻烦。

我发现一个类似的问题 Gridlayout + ScrollView。我尝试了那种方法,但没有成功。

它会剪裁许多表格(因为它应该显示从 1 到 20 的所有表格)。

这是 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:padding="16dp" >

            <android.support.v7.widget.GridLayout
                android:id="@+id/table_mapGrid"
                android:layout_width="250dp"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>

    <include layout="@layout/cell_list_loading" />

    <TextView
        android:id="@+id/table_errorView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="20dp"
        android:text="@string/message_error_connection"
        android:visibility="invisible" />

</FrameLayout>

我希望展示动态内容,可能需要在表格之间加入空白以改变列数和行数。 我已经实现了这一点,但问题是当GridLayout的宽度大于其容器的宽度时,我想使用水平滚动来解决它,但似乎并不起作用... 有什么建议吗?
2个回答

11

我找到了解决方案。

似乎安卓的ScrollView只能作为VerticalScrollView使用(这个名字不太直观,不像HorizontalScrollView)。

所以要使某些内容同时在垂直和水平方向上可滚动,你需要将一个(Vertical)ScrollView嵌套在一个HorizontalScrollView内部,或者反过来,就像这样:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

            <!-- Your content here -->

     </HorizontalScrollView>
</ScrollView>

在Android中有没有任何编程方式可以实现它? - android_dev
是的,你只需要以编程方式创建与上面相同的内容。 mScrollView.addView(mHorizontalScrollView); mHorizontalScrollView.addView(yourContentHere); - Nivaldo Bondança

1

嵌套的HorizontalScrollView / ScrollView不允许您同时在两个方向上滚动。我曾经遇到过这个问题,并为此创建了一个自定义组件,如果有帮助的话,这是链接:

https://gist.github.com/androidseb/9902093


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接