【问题标题】:Scroll view containing linearlayouts don't work包含线性布局的滚动视图不起作用
【发布时间】:2012-11-30 08:36:01
【问题描述】:

为什么我的滚动视图不滚动?

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dip"
        android:layout_marginTop="20dip"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dip"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip" >

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b1"
                android:onClick="b1" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b2"
                android:onClick="b2" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b3"
                android:onClick="b3" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b4"
                android:onClick="b4" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/b5"
                android:onClick="b5" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dip"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="10dip" >

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b6"
                android:onClick="b6" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b7"
                android:onClick="b7" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b8"
                android:onClick="b8" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b9"
                android:onClick="b9" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/b10"
                android:onClick="b10" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dip"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="10dip" >

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b11"
                android:onClick="b11" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b12"
                android:onClick="b12" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b13"
                android:onClick="b13" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b14"
                android:onClick="b14" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/b15"
                android:onClick="b15" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dip"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="10dip" >

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b16"
                android:onClick="b16" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b17"
                android:onClick="b17" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b18"
                android:onClick="b18" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b19"
                android:onClick="b19" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/b20"
                android:onClick="b20" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="80dip"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="10dip" >

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b21"
                android:onClick="b21" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b22"
                android:onClick="b22" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b23"
                android:onClick="b23" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dip"
                android:layout_weight="1"
                android:background="@drawable/b24"
                android:onClick="b24" />

            <Button
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/b25"
                android:onClick="b25" />
        </LinearLayout>
    </LinearLayout>

</ScrollView>

有什么想法吗?

【问题讨论】:

  • 你确定有东西可以滚动吗?
  • 并尝试使用android:fillViewport="true" 作为您的ScroolView

标签: android scrollview android-scrollview


【解决方案1】:

ScrollView 不能容纳超过 1 个子视图。它需要 1 个承载所有其他视图的单个子视图。希望对您有所帮助

<ScrollView>
<LinearLayout
android:id="@+id/child">
<ImageView/>
</LinearLayout>
</ScrollView>

【讨论】:

  • 我猜你的意思是ScrollView?!因为LinearLayout绝对可以容纳不止一个孩子。此外,作者发布的代码在 sv 中已经只包含一个子视图。
【解决方案2】:

一切正常,只是没有足够的按钮来启用滚动(你不需要它)。
要查看滚动是如何工作的,请使用更小的屏幕尺寸,或使用buttons 添加更多Linearlayouts

【讨论】:

    【解决方案3】:

    我不确定它是否有效,但您也可以尝试将 android:scrollbars="vertical" 属性添加到您的 LinearLayout

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多