【问题标题】:Why does ScrollView doesn't scroll even if it has text to show为什么 ScrollView 即使有要显示的文本也不滚动
【发布时间】:2016-07-07 08:07:43
【问题描述】:

我正在为 ScrollView 苦苦挣扎,并没有真正看出它有什么问题。

-我的目标是用图像和文本(在每个图像下)制作一个可滚动的片段。我已经添加了一些简单的图像,并试图在这些图像下为文本添加一个专用的 ScrollView。

问题解释从这里开始: 当片段只有 1 个图像和 1 个 ScrollView 时,ScrollView 工作正常,但是当我在下面再添加一个图像时,ScrollView 只是冻结并且不再滚动。我的错误在哪里?

代码如下:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:fillViewport="true">
<LinearLayout
    android:id="@+id/LinearLayout_Parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/top_image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image1"
        android:adjustViewBounds="true"/>
        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="100dp">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/rly_large"/>
        </ScrollView>
    //when ImageView is added ScrollView stops wotking
    **<ImageView
        android:id="@+id/top_image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image2"/>**
</LinearLayout>

Whitout ImageView inside ** 一切正常,但是当添加 ImageView 时,ScrollView 停止滚动。

我希望你能指出错误,希望没有提出愚蠢的问题。

非常感谢。

Android 工作室:2.1.2 JRE 1.8.0-b15

【问题讨论】:

标签: java android xml scrollview


【解决方案1】:

在另一个ScrollView 中包含ScrollView 不是一个好习惯。但是,您使用的代码在某些情况下可以工作。但是当你有多个元素时它可能会失败。在这种情况下,它可能无法识别gesture

但是,如果对 ScrollView Inside ScrollView 有帮助,您可以尝试此答案。当识别到孩子的触摸时,它会禁用父 ScrollView's 触摸。如果有帮助,也看看这个post

【讨论】:

  • 感谢您抽出宝贵的时间,我将尝试在其中实施,看看它是否有效。再次感谢您。
【解决方案2】:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<ScrollView 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:fillViewport="true">
<LinearLayout
    android:id="@+id/LinearLayout_Parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/top_image1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image1"
        android:adjustViewBounds="true"/>
        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="100dp">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/rly_large"/>
        </ScrollView>

    <ImageView
        android:id="@+id/top_image2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/image2"/>
</LinearLayout>

您使用 ScrollView 作为根元素,但从不添加关闭标记。 (这可能是复制粘贴问题)

另外,在我的经验中,使用 ScrollView 作为根元素会导致问题,所以如果你将当前是根元素的 ScrollView 推入一层并将 LinearLayout 设置为根元素,这可能会解决你的问题。

另外:

Whitout ImageView inside ** 一切正常,但是当添加 ImageView 时,ScrollView 停止滚动。

有点难以理解。两者中哪一个停止滚动?

【讨论】:

  • 我的错,停止滚动的是ImageView下面的那个。
  • 是否需要滚动?文本是否足够短以适合图像正下方,还是需要长时间滚动?
  • 文本会很长,所以我认为有一个小的滚动框会比一个大的文本框更好。
  • 您可以尝试在第二张图片下添加另一个滚动视图以获得更多空间并再次测试滚动视图。或者直接说 android:layout_marginBottom="100dp"
猜你喜欢
  • 1970-01-01
  • 2011-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多