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

发布于 2024-08-04 02:27:26 字数 209 浏览 10 评论 0原文

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

我正在使用 Android SDK 1.5 r3。我已经尝试过android:scrollbars="horizo​​ntal"

我在一些论坛上读到,在纸杯蛋糕更新中,水平滚动是可能的。

如何使我的布局双向滚动?

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.

I am using Android SDK 1.5 r3. I have already tried 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

雨后咖啡店 2024-08-11 02:27:26

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

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

I was able to find a simple way to achieve both scrolling behaviors.

Here is the xml for it:

<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>
披肩女神 2024-08-11 02:27:26

为时已晚,但我希望您的问题能通过此代码快速解决。
没什么可做的,只需将您的代码放在滚动视图下面即可。

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

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>
朱染 2024-08-11 02:27:26

在这篇文章中在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);

In this post Scrollview vertical and horizontal in android they talk about a possible solution, quoting:

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

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:

Solution: Replace the statement on line 808 by the following:

final int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(lp.leftMargin + lp.rightMargin, MeasureSpec.UNSPECIFIED);
半仙 2024-08-11 02:27:26

由于其他解决方案很旧,要么工作不佳,要么根本无法工作,我修改了 NestedScrollView,它稳定、现代,并且具有您对滚动视图的所有期望。除了水平滚动。

这是仓库: https://github.com/ultimate-deej/TwoWayNestedScrollView

我已经除了绝对必要的内容之外,没有对原始 NestedScrollView 进行任何更改或“改进”。
该代码基于 androidx.core:core:1.3.0,这是撰写本文时的最新稳定版本。

以下所有功能均有效:

  • 滚动时提升(因为它基本上是一个 NestedScrollView
  • 两个维度中的边缘效果
  • 在两个维度中填充视口

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. Except for horizontal scrolling.

Here's the repo: 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:

  • Lift on scroll (since it's basically a NestedScrollView)
  • Edge effects in both dimensions
  • Fill viewport in both dimensions
睫毛溺水了 2024-08-11 02:27:26

使用这个:

android:scrollbarAlwaysDrawHorizontalTrack="true"

示例:

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

Use this:

android:scrollbarAlwaysDrawHorizontalTrack="true"

Example:

<Gallery android:id="@+id/gallery"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbarAlwaysDrawHorizontalTrack="true" />
仅此而已 2024-08-11 02:27:26

您可以使用下面的代码来做到这一点

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

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>
弱骨蛰伏 2024-08-11 02:27:26

此实现始终可以显示水平和垂直滚动条

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

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);
        }
    });
花期渐远 2024-08-11 02:27:26

遇到了同样的问题(SDK 34),但是对于 AppBarLayout,我没有找到有效的解决方案。最后,这个解决方案适用于我的大型 Tablelayout 和 AppBarLayout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"   
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">
    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
            
        // use Table- or other Layout here with:    
        // android:layout_width="wrap_content"
        // android:layout_height="wrap_content"         
    </HorizontalScrollView>
</androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Had the same problem (SDK 34), but with AppBarLayout no solution i found works. Finally, this solution works with my large Tablelayout and AppBarLayout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"   
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" >

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">
    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>

<androidx.core.widget.NestedScrollView
    android:fillViewport="true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
            
        // use Table- or other Layout here with:    
        // android:layout_width="wrap_content"
        // android:layout_height="wrap_content"         
    </HorizontalScrollView>
</androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文