我正在使用TableLayout。我需要为此布局同时进行水平和垂直滚动。默认情况下,我可以在视图中进行垂直滚动,但水平滚动不起作用。
我使用的是Android SDK 1.5 r3。我已经尝试过android:scrollbars="horizontal"
。
我在一些论坛上看到,在杯形蛋糕更新中,可以进行水平滚动。
如何让我的布局向两个方向滚动?
答案 0 :(得分:129)
我能够找到一种简单的方法来实现两种滚动行为。
以下是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 xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linlay" android:layout_width="320px"
android:layout_height="fill_parent" android:stretchColumns="1"
android:background="#000000"/>
</HorizontalScrollView>
</ScrollView>
答案 1 :(得分:5)
为时已晚,但我希望您的问题能够通过此代码快速解决。 没有什么可以做的,只需将你的代码放在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>
答案 2 :(得分:1)
在这篇文章Scrollview vertical and horizontal in android中,他们谈到了一个可能的解决方案,引用:
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);
答案 3 :(得分:0)
使用此:
android:scrollbarAlwaysDrawHorizontalTrack="true"
示例:
<Gallery android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="true" />
答案 4 :(得分:0)
由于其他解决方案已经过时,或者工作不佳或根本无法工作,因此我修改了NestedScrollView
,它是稳定,现代的,它具有滚动视图中您所期望的一切。除了水平滚动。
这是仓库:https://github.com/ultimate-deej/TwoWayNestedScrollView
对于绝对必要的内容,我没有做出任何更改,也没有对原始NestedScrollView
进行任何“改进”。
该代码基于androidx.core:core:1.3.0
,它是撰写本文时的最新稳定版本。
以下所有作品:
NestedScrollView
)答案 5 :(得分: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>