You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Android Activity拆分为两部分且第一部分可滚动

嘿,我来帮你搞定这个需求!要实现一个上半部分可滚动、下半部分固定不动的Android Activity,其实有两种简单又实用的方案,我给你一步步拆解:

方案一:LinearLayout + ScrollView 基础实现

这是最直观的方式,把需要滚动的内容塞进ScrollView,固定部分直接放在ScrollView外面,让LinearLayout垂直排列即可。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- 可滚动的上半部分 -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <!-- ScrollView只能有一个直接子布局,这里用LinearLayout来放所有滚动内容 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <!-- 这里放你的滚动内容,比如文本、图片、列表项等 -->
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="这里是可滚动的内容区域,你可以添加任意多的控件,比如长文本、图片、表单输入框等等,超出屏幕高度后就会自动滚动啦!"
                android:textSize="16sp"
                android:lineSpacingExtra="8dp"/>
            
            <!-- 可以多添加几个控件模拟长内容 -->
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="更多滚动内容..."
                android:textSize="16sp"
                android:layout_marginTop="20dp"/>
            <!-- 重复添加或者用真实业务内容替换 -->

        </LinearLayout>
    </ScrollView>

    <!-- 固定不动的下半部分 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f0f0f0"
        android:padding="16dp"
        android:orientation="horizontal">

        <!-- 这里放固定的控件,比如按钮、操作栏等 -->
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="确认"/>
        
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="取消"
            android:layout_marginStart="16dp"/>

    </LinearLayout>

</LinearLayout>
方案二:ConstraintLayout + NestedScrollView 灵活实现

如果你的布局更复杂,ConstraintLayout能提供更精准的位置控制,搭配NestedScrollView还能支持和CoordinatorLayout的联动(比如配合AppBarLayout做折叠效果)。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 可滚动的上半部分 -->
    <androidx.core.widget.NestedScrollView
        android:id="@+id/scroll_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/fixed_content">

        <!-- 同样,NestedScrollView只能有一个直接子布局 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="16dp">

            <!-- 滚动内容区域,和方案一一样可以添加任意控件 -->
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="这是用ConstraintLayout实现的滚动区域,布局更灵活,适合复杂界面需求"
                android:textSize="16sp"
                android:lineSpacingExtra="8dp"/>

            <!-- 模拟长内容 -->
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="更多滚动内容..."
                android:textSize="16sp"
                android:layout_marginTop="20dp"/>

        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

    <!-- 固定不动的下半部分 -->
    <LinearLayout
        android:id="@+id/fixed_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#f0f0f0"
        android:padding="16dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent">

        <!-- 固定控件,比如操作按钮 -->
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="提交"/>
        
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="返回"
            android:layout_marginStart="16dp"/>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
一些注意事项
  • ScrollView/NestedScrollView只能有一个直接子布局,所以一定要用LinearLayout、RelativeLayout等容器把所有滚动内容包起来,不然会报错。
  • 如果你的滚动内容里有RecyclerView,一定要用NestedScrollView,同时给RecyclerView设置android:nestedScrollingEnabled="false",避免滚动冲突。
  • 固定部分的高度可以根据需求设置为wrap_content(自适应内容)或者固定值(比如100dp)。

内容的提问来源于stack exchange,提问作者Hiren A. Vyas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:34:08