如何在安卓中实现表格布局的横向和纵向滚动

27

我对表格布局滚动感到非常困惑。我需要实现带有水平和垂直滚动的表格。我也看过表格固定标题的例子,但tablefixheader示例使用适配器来设置数据,而我需要在表格布局中使用add-view方法。 我尝试了下面的代码,但它无法支持两种滚动方式。

      <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="vertical" >

        <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="fill_parent" 
            android:fadeScrollbars="false">

            <TableLayout
                android:id="@+id/tableLayoutId"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </HorizontalScrollView>
    </ScrollView>
2个回答

69

这是我实现的方式,对我来说有效:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
 <ScrollView 
    android:id="@+id/layout" 
    android:layout_height="match_parent"         
    android:scrollbars="horizontal|vertical" 
    android:layout_width="match_parent"     
    android:layout_marginTop="5dip"     
    android:scrollbarStyle="outsideInset"
    android:fillViewport="true"> 

    <HorizontalScrollView 
        android:id="@+id/horizontalView" 
        android:layout_height="wrap_content"     
        android:scrollbars="horizontal|vertical" 
        android:layout_width="wrap_content"     
        android:layout_marginTop="5dip">

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/tlGridTable" >   
        </TableLayout>
    </HorizontalScrollView>
</ScrollView>
</LinearLayout>

看一下这段代码,看看是否有所帮助。


1
很好 +1。你应该加上一点解释为什么它有效 =) - user2336315
我的猜测是它能够工作,因为在两个scrollView中都有这一行android:scrollbars="horizontal|vertical",而这是OP所忽略的。 - Emil Adz
你应该将 outsideInset 的值更改为 outsideOverlay,否则 TableLayout 的宽度将不会匹配父容器的宽度。它无法填满整个屏幕。 - Hampel Előd
我在ScrollView中遇到了一些问题,请帮帮我,我尝试了你的解决方案,但没有成功。@EmilAdz http://stackoverflow.com/questions/38588702/why-my-scrollview-not-working-properly - Karthi
@KarthiVenture,你提出的问题与这段代码无关。这段代码旨在实现水平和垂直滚动,而你只想应用垂直滚动。 - Emil Adz
好的,谢谢兄弟。但是我该如何解决我的问题?我犯了什么错误?@EmilAdz - Karthi

5

像这样添加

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

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

            <android.support.v7.widget.RecyclerView
                android:id="@+id/rv_pins"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scrollbarStyle="outsideOverlay"
                android:scrollbars="horizontal" />
        </HorizontalScrollView>

    </ScrollView>

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