Android 如何让我的布局水平和垂直滚动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1399605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make my layout scroll both horizontally and vertically?
提问by mudit
I am using a TableLayout. I need to have both horizontal and vertical scrolling for this layout. By default I am able to get vertical scrolling in the view but horizontal scrolling is not working.
我正在使用 TableLayout。我需要为此布局同时进行水平和垂直滚动。默认情况下,我可以在视图中进行垂直滚动,但水平滚动不起作用。
I am using Android SDK 1.5 r3. I have already tried android:scrollbars="horizontal"
.
我正在使用 Android SDK 1.5 r3。我已经试过了android:scrollbars="horizontal"
。
I have read on some forums that in the cupcake update, horizontal scrolling is possible.
我在一些论坛上读到过,在纸杯蛋糕更新中,可以进行水平滚动。
How can I make my layout scroll in both directions?
如何让我的布局在两个方向滚动?
回答by mudit
I was able to find a simple way to achieve both scrolling behaviors.
我能够找到一种简单的方法来实现这两种滚动行为。
Here is the xml for it:
这是它的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>
回答by Ashish Tikarye
its too late but i hope your issue will be solve quickly with this code. nothing to do more just put your code in below 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>
回答by neteinstein
In this post Scrollview vertical and horizontal in androidthey talk about a possible solution, quoting:
在这篇文章android 中的垂直和水平滚动视图中,他们讨论了一个可能的解决方案,引用:
Matt Clark has built a custom view based on the Android source, and it seems to work perfectly: http://blog.gorges.us/2010/06/android-two-dimensional-scrollview
马特克拉克已经建立了一个基于Android源的自定义视图,它似乎完美地工作:http: //blog.gorges.us/2010/06/android-two-dimensional-scrollview
Beware that the class in that page has a bug calculating the view's horizonal width. A fix by Manuel Hilty is in the comments:
请注意,该页面中的类在计算视图的水平宽度时存在错误。Manuel Hilty 的修复在评论中:
Solution: Replace the statement on line 808 by the following:
解决方案:将第 808 行的语句替换为以下内容:
final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.leftMargin + lp.rightMargin, MeasureSpec.UNSPECIFIED);
回答by Patel Ekta
Use this:
用这个:
android:scrollbarAlwaysDrawHorizontalTrack="true"
Example:
例子:
<Gallery android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="true" />