[英]How can I make my layout scroll both horizontally and vertically?
I am using a TableLayout.我正在使用 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.
默认情况下,我可以在视图中进行垂直滚动,但水平滚动不起作用。
I am using Android SDK 1.5 r3.我正在使用 Android SDK 1.5 r3。 I have already tried
android:scrollbars="horizontal"
.我已经尝试过
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?如何让我的布局在两个方向滚动?
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
android:id="@+id/linlay" android:layout_width="320px"
android:layout_height="fill_parent" android:stretchColumns="1"
android:background="#000000"/>
</HorizontalScrollView>
</ScrollView>
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>
In this post Scrollview vertical and horizontal in android they 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);
Since other solutions are old and either poorly-working or not working at all, I've modified NestedScrollView
, which is stable, modern and it has all you expect from a scroll view.由于其他解决方案很旧,要么效果不佳,要么根本无法使用,我修改了
NestedScrollView
,它稳定、现代,并且具有您对滚动视图的所有期望。 Except for horizontal scrolling.除了水平滚动。
Here's the repo: https://github.com/ultimate-deej/TwoWayNestedScrollView这是回购: https : //github.com/ultimate-deej/TwoWayNestedScrollView
I've made no changes, no "improvements" to the original NestedScrollView
except for what was absolutely necessary.除了绝对必要的
NestedScrollView
外,我没有对原始NestedScrollView
进行任何更改和“改进”。 The code is based on androidx.core:core:1.3.0
, which is the latest stable version at the time of writing.代码基于
androidx.core:core:1.3.0
,这是撰写本文时最新的稳定版本。
All of the following works:以下所有工作:
NestedScrollView
)NestedScrollView
)Use this:用这个:
android:scrollbarAlwaysDrawHorizontalTrack="true"
Example:例子:
<Gallery android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="true" />
You can do this by using below code您可以使用以下代码来执行此操作
<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>
This implementation can always display both horizontal and vertical scrollbars此实现始终可以同时显示水平和垂直滚动条
activity_main.xml活动_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主活动.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);
}
});
The problem with most of the answers here is that they only achieve vertical or horizontal scroll, BUT not diagonal (horizontal AND vertical at the same time).这里大部分的答案的问题是,他们只实现水平或垂直滚动,而不是对角(水平和垂直的同时)。
The easiest way I've found to get it working is:我发现让它工作的最简单方法是:
HorizontalScrollView
and ScrollView
(use whatever name, like "ScrollViewX" and "ScrollViewY").HorizontalScrollView
和ScrollView
新类(使用任何名称,如“ScrollViewX”和“ScrollViewY”)。HorizontalScrollView
inside the one extending ScrollView
.ScrollView
使用扩展HorizontalScrollView
的类。onTouch
methods to always return false
(override and use performClick()
too, for accessibility reasons).onTouch
方法以始终返回false
(出于可访问性原因,也重写并使用performClick()
)。GestureDetector
and update the scrolling of both classes with it.GestureDetector
并使用它更新两个类的滚动。 Creating the classes isn't really required, since you could just set an onTouch
listener for both.创建类并不是真正必需的,因为您可以为两者设置一个
onTouch
侦听器。 But I believe it's better in terms of organization and it may be useful if you require more customizations later.但我相信它在组织方面更好,如果您以后需要更多自定义,它可能会很有用。
The only problem I've found is that, since one ScrollView is inside the other, now one of the scrollbars is only going to be visible when you scroll while being on the edge of the view.我发现的唯一问题是,由于一个 ScrollView 在另一个内部,现在只有当您在视图边缘滚动时才能看到其中一个滚动条。
A workaround could be: either don't use scrollbars, or make a custom view to replace said scrollbar (you set its size according to the ScrollView size and move it as the scroll is updated).解决方法可能是:要么不使用滚动条,要么创建自定义视图来替换所述滚动条(根据 ScrollView 大小设置其大小并在滚动更新时移动它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.