我正在使用表格布局。我需要为此布局进行水平和垂直滚动。默认情况下,我可以在视图中进行垂直滚动,但水平滚动不起作用。
我正在使用 Android SDK 1.5 r3。我已经试过了android:scrollbars="horizontal"
。
我在一些论坛上读到,在纸杯蛋糕更新中,水平滚动是可能的。
如何让我的布局双向滚动?
我正在使用表格布局。我需要为此布局进行水平和垂直滚动。默认情况下,我可以在视图中进行垂直滚动,但水平滚动不起作用。
我正在使用 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>
在这篇文章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);
由于其他解决方案陈旧,要么工作不佳,要么根本不工作,我已经修改了NestedScrollView
,它是稳定的、现代的,并且它具有您对滚动视图的所有期望。横向滚动除外。
这是回购:https ://github.com/ultimate-deej/TwoWayNestedScrollView
I've made no changes, no "improvements" to the original NestedScrollView
except for what was absolutely necessary.
The code is based on androidx.core:core:1.3.0
, which is the latest stable version at the time of writing.
All of the following works:
NestedScrollView
)用这个:
android:scrollbarAlwaysDrawHorizontalTrack="true"
例子:
<Gallery android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbarAlwaysDrawHorizontalTrack="true" />
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
inside the one extending ScrollView
.onTouch
methods to always return false
(override and use performClick()
too, for accessibility reasons).GestureDetector
and update the scrolling of both classes with it.Creating the classes isn't really required, since you could just set an onTouch
listener for both. 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.
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).
Note: I tried to describe it as much as I could. If someone needs more details, let me know
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
<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
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);
}
});