如何使我的布局可以水平和垂直滚动?

82
我正在使用TableLayout。我需要该布局的水平滚动和垂直滚动。默认情况下,我能够在视图中获得垂直滚动,但是水平滚动无法正常工作。我正在使用Android SDK 1.5 r3。我已经尝试过android:scrollbars="horizontal"。我在一些论坛上读到,在杯子蛋糕更新中,可以进行水平滚动。如何使我的布局在两个方向上滚动?
7个回答

151

我找到了一种简单的方法来实现两种滚动行为。

这是它的xml代码:

<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="320px" android:layout_height="fill_parent">

        <TableLayout
            android:id="@+id/linlay" android:layout_width="320px"
            android:layout_height="fill_parent" android:stretchColumns="1"
            android:background="#000000"/>

    </HorizontalScrollView>

</ScrollView>

4
警告:这可能是一种不良实践。请参考此问题/答案: https://dev59.com/-m855IYBdhLWcg3wKxHc - Pedro Loureiro
7
@PedroLoureiro实际上,你发布链接中的问题是关于将ScrollViewListView结合使用。这是因为ListView内置了许多优化功能,这些功能与周围的ScrollView不兼容。这里发布的答案没有使用ListView,因此链接中的论点无关紧要。 - David Wasser
有没有其他的方法可以实现固定列和行?使用固定视图滚动不够流畅。 - Aditi Parikh
2
我认为这是一个好主意,但实际上它不允许对角线滚动 - 您只能一次滚动一个轴.. 这有点糟糕。 - Maverick Meerkat
1
对角线滚动不起作用。 - ievgen
显示剩余2条评论

9

虽然有点晚了,但我希望这段代码能快速解决你的问题。 只需将你的代码放在下面的滚动视图中即可。

<HorizontalScrollView
        android:id="@+id/scrollView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

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

2
在这篇文章“在Android中实现ScrollView的垂直和水平滚动”中,他们讨论了一个可能的解决方案,引用如下:
Matt Clark基于Android源代码构建了一个自定义视图,它似乎完美地工作:http://blog.gorges.us/2010/06/android-two-dimensional-scrollview 请注意,该页面上的类存在计算视图水平宽度的错误。Manuel Hilty在评论中提供了修复方法:
解决方案:将第808行的语句替换为以下内容:
final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.leftMargin + lp.rightMargin, MeasureSpec.UNSPECIFIED);

2

由于其他解决方案都过时,要么效果不佳,要么根本不起作用,因此我修改了NestedScrollView,它稳定、现代化,并且具备滚动视图所期望的所有功能。除了水平滚动。

这是仓库:https://github.com/ultimate-deej/TwoWayNestedScrollView

除了绝对必要的更改外,我没有对原始的NestedScrollView进行任何“改进”。代码基于androidx.core:core:1.3.0,这是撰写本文时的最新稳定版本。

以下所有内容均可正常工作:

  • 随着滚动而上升(因为它基本上是一个NestedScrollView
  • 在两个维度上具有边缘效果
  • 在两个维度上填充视口

1
使用这个:
android:scrollbarAlwaysDrawHorizontalTrack="true"

例子:

<Gallery android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbarAlwaysDrawHorizontalTrack="true" />

0

这个实现可以始终显示水平和垂直滚动条

activity_main.xml

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

    <!-- vertical scroll view -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:fadeScrollbars="false"
        android:scrollbarSize="@dimen/scroll_bar_size">

        <!-- horizontal scroll view hidden scroll bar -->
        <HorizontalScrollView
            android:id="@+id/real_horizontal_scroll_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            android:scrollbars="none">

            <!-- content view -->
            <EditText
                android:id="@+id/real_inside_view"
                android:layout_width="wrap_content"
                android:layout_height="match_parent" />
        </HorizontalScrollView>
    </ScrollView>

    <!-- fake horizontal scroll bar -->
    <HorizontalScrollView
        android:id="@+id/fake_horizontal_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:fadeScrollbars="false"
        android:scrollbarSize="@dimen/scroll_bar_size">

        <!-- fake content view that has width equals the real content view -->
        <View
            android:id="@+id/fake_inside_view"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/scroll_bar_size" />
    </HorizontalScrollView>
</RelativeLayout>

MainActivity.java

    final EditText realInsideView = findViewById(R.id.real_inside_view);
    final HorizontalScrollView realHorizontalSv = findViewById(R.id.real_horizontal_scroll_view);

    final View fakeInsideView = findViewById(R.id.fake_inside_view);
    final HorizontalScrollView fakeHorizontalSv = findViewById(R.id.fake_horizontal_scroll_view);

    realHorizontalSv.setOnScrollChangeListener(new View.OnScrollChangeListener() {
        @Override
        public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
            fakeInsideView.setMinimumWidth(realInsideView.getWidth());
            fakeHorizontalSv.setScrollX(scrollX);
        }
    });

0
您可以使用以下代码来实现此功能。
<HorizontalScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <ScrollView
                        android:layout_width="wrap_content"
                        android:layout_height="match_parent">
                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">
                            
                        </LinearLayout>
                    </ScrollView>
    </HorizontalScrollView>

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