如何使一个LinearLayout可滚动?

273

我在屏幕上有很多项目,需要使用滚动条让用户可以向下滚动。但是,滚动条要么不可见,要么不起作用。如何向LinearLayout添加滚动条呢?


4
可能是如何使LinearLayout可滚动的重复问题。 - Narkha
10个回答

490

使用 <ScrollView> 包裹线性布局:

点击此处查看示例:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       xmlns:android="http://schemas.android.com/apk/res/android">
       <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <LinearLayout 
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
                  <!-- Content here -->
            </LinearLayout>
      </ScrollView>
 </LinearLayout>

注意:在API级别8及更高版本中,fill_parent已被弃用并更名为match_parent


1
@DanielMagnusson 这里链接仍然有效...这是一个视频指南:http://www.youtube.com/watch?v=kNX996ZZ2CI - Bryan Denny
36
我认为你不需要外部的LinearLayout。 - Lawrence Kesteloot
1
@Lawrence 不是必须的,但这取决于你的视图的其余部分是什么样子。 - Bryan Denny
1
如果您只在顶部线性布局中有ScrollView,则会收到警告“此ScrollView布局或其LinearLayout父级是无用的;将背景属性转移到其他视图”,这意味着仅有一个项目的线性布局是没有意义的。 - Niels
3
重要提示:ScrollView 组件必须仅包含一个子组件。 - O-9
显示剩余5条评论

154
<ScrollView 
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/scroll" 
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

      <LinearLayout 
            android:id="@+id/container"
            android:orientation="vertical" 
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
      </LinearLayout>

 </ScrollView>

8
你需要使用 ScrollView 包裹你的 LinearLayout。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout 
        android:id="@+id/container" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


    </LinearLayout>
</ScrollView>

4
这可以通过使用标签<ScrollView>来实现。对于ScrollView,有一件事情需要提醒,就是ScrollView必须只有一个子元素
如果您想要整个布局都可以滚动,请在顶部添加<ScrollView>。请查看下面的示例。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scroll" 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout 
        android:id="@+id/container" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
            <!-- Content here -->
    </LinearLayout>

</ScrollView>

但如果您希望布局的某个部分可滚动,则在该部分中添加<ScrollView>。请查看下面给出的示例。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="400dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
                <!-- Content here -->
        </LinearLayout>

    </ScrollView>

</LinearLayout>

4
这是我通过试错方法完成的步骤。
ScrollView - (the outer wrapper).

    LinearLayout (child-1).

        LinearLayout (child-1a).

        LinearLayout (child-1b).

由于ScrollView只能有一个子元素,所以该子元素是线性布局。然后,所有其他布局类型都出现在第一个线性布局中。我还没有尝试包含相对布局,因为它们让我发疯,所以我会等到我的理智回来再尝试。


能否修复一下格式?你的第一行代码没有被包含在代码段中,但因为这个修改太微不足道了,所以网站不允许我进行修正。 - Caltor

2
你需要使用以下属性,并将其包含在线性布局内。
<LinearLayout ...>
<scrollView ...> 

</scrollView>
</LinearLayout>

1
 <LinearLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
                <---------Content Here --------------->
            </LinearLayout>
       </ScrollView>
    </LinearLayout>

1

您需要将 ScrollView 放置在布局文件的第一个子元素位置,然后再将线性布局放置在其中。现在,Android 将根据内容和设备可用大小来决定是否显示可滚动条。

请确保线性布局没有兄弟元素,因为 ScrollView 不能有多个子元素。


0

每当您想要使布局可滚动时,您可以使用<ScrollView>将其包含在其中。


-29
你可以在LinearLayout中添加一个属性:android:scrollbars="vertical"

4
这只控制滚动条的可见性,但它不会为您创建一个滚动视图。 - Botond Kopacz

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