繁体   English   中英

如何让我的布局水平和垂直滚动?

[英]How can I make my layout scroll both horizontally and vertically?

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

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

我在一些论坛上读到过,在纸杯蛋糕更新中,可以进行水平滚动。

如何让我的布局在两个方向滚动?

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

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

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

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

在这篇文章android 中的垂直和水平滚动视图中,他们讨论了一个可能的解决方案,引用:

马特克拉克已经建立了一个基于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);

由于其他解决方案很旧,要么效果不佳,要么根本无法使用,我修改了NestedScrollView ,它稳定、现代,并且具有您对滚动视图的所有期望。 除了水平滚动。

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

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

以下所有工作:

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

用这个:

android:scrollbarAlwaysDrawHorizontalTrack="true"

例子:

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

您可以使用以下代码来执行此操作

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

此实现始终可以同时显示水平和垂直滚动条

活动_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>

主活动.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);
        }
    });

这里大部分的答案的问题是,他们只实现水平或垂直滚动,而不是对角(水平和垂直的同时)。

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM