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

问题描述 投票:0回答:8

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

我使用的是 Android SDK 1.5 r3。我已经尝试过

android:scrollbars="horizontal"

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

如何让布局双向滚动?

android android-widget android-scrollview
8个回答
152
投票

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

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

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

在这篇文章中Scrollview Vertical and Horizon in android他们讨论了一个可能的解决方案,引用:

Matt Clark 基于 Android 源代码构建了一个自定义视图,并且似乎运行良好:http://blog.gorges.us/2010/06/android-two-Dimension-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
  • 两个维度的边缘效应
  • 在两个维度上填充视口

1
投票

NestedScrollView

示例:

android:scrollbarAlwaysDrawHorizontalTrack="true"



0
投票

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



0
投票

activity_main.xml

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

MainActivity.java

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



0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.