如何使LinearLayout可滚动?

Tro*_*roj 232 android android-linearlayout

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

Bry*_*nny 442

用a包裹线性布局 <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>
Run Code Online (Sandbox Code Playgroud)

  • 我认为你不需要外部的LinearLayout. (31认同)
  • 和一些注意事项:ScrollView必须只有一个孩子 (2认同)

kru*_*hah 140

<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>
Run Code Online (Sandbox Code Playgroud)


Nju*_*_nc 8

您需要用滚动视图包装线性布局

<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>
Run Code Online (Sandbox Code Playgroud)