你如何使LinearLayout滚动?

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

<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> 
 <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> 

这是我通过反复试验完成的。

ScrollView – (外部包装)。

  LinearLayout (child-1). LinearLayout (child-1a). LinearLayout (child-1b 

由于滚动视图只能有一个孩子,该孩子是一个线性布局。 然后所有其他布局types出现在第一个线性布局中。 我还没有试图包括一个相对的布局,但他们迫使我疯了,所以我会等到我的理智恢复。

这可以使用标签<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> 
  <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"`enter code here`> <---------Content Here ---------------> </LinearLayout> </ScrollView> </LinearLayout> 

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

确保linearlayout没有兄弟,因为ScrollView不能有多个孩子。

在使用Eclipse ADT时:graphics布局 – >复合 – > ScrollView

你可以在linearLayout中添加一个atrribute: android:scrollbars="vertical"