屏幕大小的水平滚动视图内部的布局

13
我是Android的新手,一直在寻找解决方案,但迄今为止没有运气。我想创建一个布局,就像下面的图片一样。

我想要一个大小与屏幕相同的LinearLayout。然后有另一个大小也与屏幕相同但不在屏幕上的LinearLayout。我可以在这两个虚拟“屏幕”之间滚动。

enter image description here

有一篇有趣的文章讲解了如何扩展scrollView类,以便我可以获得炫酷的捕捉效果,因此,如果我能使其工作,我的应用程序将感觉就像在主屏幕之间滚动。

我已经了解了权重和scrollView的fillViewport =“true”,但我害怕我不知道如何使用这些东西来让horizontalScrollView填充屏幕。我尝试过各种fill_parent和wrap_content的组合,但都无济于事。

在我看来,只要针对屏幕可变性构建子视图(每个“屏幕”中的元素),这个功能就不会损害在不同屏幕设备之间的应用程序可移植性。

这是我尝试的XML的简单示例:

<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/HorizontalScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true">

<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <EditText
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/txtTestBox"
            >
        </EditText>
    </LinearLayout>

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Button 1"
        />

    </LinearLayout>

</LinearLayout>

</HorizontalScrollView>

很不幸,那甚至不能接近我要找的东西。希望这可以实现...

感谢任何帮助或建议。


我认为将我提到的“snap”代码链接放在这里可能会有所帮助。它可以在这里找到:http://blog.velir.com/index.php/2010/11/17/android-snapping-horizontal-scroll/ - Arbiter of buffoonery
我想要实现的功能还可以在安卓版Firefox移动应用程序中找到另一个示例。页面左侧的选项卡栏(直到滚动时才出现)似乎以某种方式创建了一个可滚动的表面,其中包含一个与屏幕完全相同大小的子视图(它创建了一个与物理手机屏幕大小相同的“屏幕”,当用户滚动时,他们可以查看他们的选项卡和屏幕)。希望这样能清楚地表达我的问题。 - Arbiter of buffoonery
3个回答

6

请点击这里 解决方案是在 ScrollView 上使用 android:fillViewport="true",不需要修复 LinearLayout 的宽度。

     <HorizontalScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:fillViewport="true"
            android:orientation="vertical"
            android:scrollbars="none">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                //create your views
            </LinearLAyout>
</HorizontalScrollView>

1
android:fillViewport="true" 解决了我的问题 - Rohan Pawar

2

一个水平滚动视图可以无限地横向缩放,因此在内部布局上使用"填充父级"可能不会像您期望的那样起作用。您是否尝试过在内部布局中显式指定宽度?

类似于以下内容:

<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/HorizontalScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true">

<LinearLayout 
    android:orientation="horizontal"
    android:layout_width="400dp"
    android:layout_height="fill_parent">

.....

</LinearLayout>


</HorizontalScrollView>

这是我现在采取的方法。我希望避免手动格式化每个视图,但似乎这是唯一的选择。谢谢! - Arbiter of buffoonery

0

你需要在一个屏幕内虚拟地拥有两个屏幕的具体原因是什么?为什么不让它们单独成为两个屏幕呢?你可以扩展GestureDetector,这样当滚动手势发生时,它会自动“捕捉”到下一个屏幕。


好问题,我对此很感兴趣,因为我认为这将允许更直观的界面。由于我的界面有点复杂,我认为能够“窥视”下一个屏幕上的内容将会很有用。此外,现在我想知道如何做到这一点,因为我知道它是可以做到的(我已经苦苦思索了一段时间,所以我不想放弃)。如果我找不到答案,我计划退而求其次,采用你提出的方案。 - Arbiter of buffoonery
顺便提一下,我所指的“snap”代码链接可以在这里找到:http://blog.velir.com/index.php/2010/11/17/android-snapping-horizontal-scroll/。 - Arbiter of buffoonery
这并不是问题的真正答案。希望有更好的答案存在。也许我可以在Firefox移动版源代码中找到答案... - Arbiter of buffoonery

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接