我在屏幕上有很多项目,需要使用滚动条让用户可以向下滚动。但是,滚动条要么不可见,要么不起作用。如何向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>
注意:在API级别8及更高版本中,fill_parent已被弃用并更名为match_parent。
<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 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>
<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>
ScrollView - (the outer wrapper).
LinearLayout (child-1).
LinearLayout (child-1a).
LinearLayout (child-1b).
由于ScrollView只能有一个子元素,所以该子元素是线性布局。然后,所有其他布局类型都出现在第一个线性布局中。我还没有尝试包含相对布局,因为它们让我发疯,所以我会等到我的理智回来再尝试。
<LinearLayout ...>
<scrollView ...>
</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">
<---------Content Here --------------->
</LinearLayout>
</ScrollView>
</LinearLayout>
您需要将 ScrollView 放置在布局文件的第一个子元素位置,然后再将线性布局放置在其中。现在,Android 将根据内容和设备可用大小来决定是否显示可滚动条。
请确保线性布局没有兄弟元素,因为 ScrollView 不能有多个子元素。
每当您想要使布局可滚动时,您可以使用<ScrollView>
将其包含在其中。
android:scrollbars="vertical"
。