【问题标题】:How can I make my layout scroll both horizontally and vertically?如何使我的布局水平和垂直滚动?
【发布时间】:2010-11-26 20:13:13
【问题描述】:

我正在使用 TableLayout。我需要为此布局进行水平和垂直滚动。默认情况下,我可以在视图中进行垂直滚动,但水平滚动不起作用。

我正在使用 Android SDK 1.5 r3。我已经尝试过android:scrollbars="horizontal"

我在一些论坛上看到,在纸杯蛋糕更新中,水平滚动是可能的。

如何让我的布局双向滚动?

【问题讨论】:

    标签: android android-widget android-scrollview


    【解决方案1】:

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

    这是它的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>
    

    【讨论】:

    • 水平滚动效果很好......但是没有可见的水平滚动条......我们怎么能这样做......?
    • 警告:这可能是一种不好的做法。请参阅此问题/答案:stackoverflow.com/questions/4490821/…
    • @PedroLoureiro 实际上,您发布的链接中的问题是指将ScrollViewListView 结合起来。这是因为ListView 有大量优化,不能很好地与周围的ScorollView 配合使用。此处发布的答案未使用ListView,因此链接参数无关紧要。
    • 我认为这是个好主意,但它实际上不允许对角滚动 - 您一次只能滚动一个轴......这有点糟糕
    • 对角滚动不能这样工作
    【解决方案2】:

    为时已晚,但我希望您的问题将通过此代码快速解决。 无需多做,只需将代码放在滚动视图下方即可。

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

    【讨论】:

      【解决方案3】:

      Scrollview vertical and horizontal in android 这篇文章中,他们谈到了一个可能的解决方案,引用:

      Matt Clark 已经基于 Android 源码构建了一个自定义视图,它似乎可以完美运行:http://blog.gorges.us/2010/06/android-two-dimensional-scrollview

      请注意,该页面中的类在计算视图的水平宽度时存在错误。 Manuel Hilty 在 cmets 中进行了修复:

      解决方案:将第 808 行的语句替换为以下内容:

      final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.leftMargin + lp.rightMargin, MeasureSpec.UNSPECIFIED);
      

      【讨论】:

        【解决方案4】:

        由于其他解决方案陈旧且工作不佳或根本不工作,我已修改NestedScrollView,它稳定、现代,并且可以满足您对滚动视图的所有期望。水平滚动除外。

        这里是回购:https://github.com/ultimate-deej/TwoWayNestedScrollView

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

        以下所有作品:

        • 滚动提升(因为它基本上是 NestedScrollView
        • 两个维度的边缘效果
        • 在两个维度上填充视口

        【讨论】:

          【解决方案5】:

          使用这个:

          android:scrollbarAlwaysDrawHorizontalTrack="true"
          

          例子:

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

          【讨论】:

            【解决方案6】:

            这里大多数答案的问题是它们只能实现垂直或水平滚动,但不是对角线(同时水平和垂直)。

            我发现让它工作的最简单方法是:

            • 创建两个扩展 HorizontalScrollViewScrollView 的新类(使用任何名称,例如“ScrollViewX”和“ScrollViewY”)。
            • 创建一个新的 XML 并在扩展 ScrollView 的类中使用扩展 HorizontalScrollView 的类。
            • 在这两个类中,覆盖它们的 onTouch 方法以始终返回 false(出于可访问性原因,也覆盖并使用 performClick())。
            • 使用GestureDetector 并使用它更新两个类的滚动。

            创建类并不是真正需要的,因为您可以为两者设置一个onTouch 侦听器。但我相信它在组织方面更好,如果您以后需要更多自定义,它可能会很有用。

            我发现的唯一问题是,由于一个 ScrollView 在另一个 ScrollView 内,现在只有当您在视图边缘滚动时,其中一个滚动条才会可见。

            解决方法可能是:要么不使用滚动条,要么制作自定义视图来替换所述滚动条(您根据 ScrollView 大小设置其大小,并在滚动更新时移动它)。

            注意:我尽量描述它。如果有人需要更多详细信息,请告诉我

            【讨论】:

              【解决方案7】:

              你可以使用下面的代码来做到这一点

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

              【讨论】:

                【解决方案8】:

                这个实现总是可以同时显示水平和垂直滚动条

                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);
                        }
                    });
                

                【讨论】:

                  猜你喜欢
                  • 2019-04-13
                  • 1970-01-01
                  • 2019-12-11
                  • 2013-05-13
                  • 1970-01-01
                  • 2016-07-13
                  • 1970-01-01
                  • 2014-01-09
                  • 1970-01-01
                  相关资源
                  最近更新 更多