You need to enable JavaScript to run this app. 如何在ScrollView中将广告线性布局对齐到页面底部
优惠活动
大模型
产品
解决方案
定价
更多

如何在ScrollView中将广告线性布局对齐到页面底部

问题原因

你之前设置的layout_alignParentBottom仅适用于RelativeLayout、ConstraintLayout类父布局,当前父容器是LinearLayout,该属性无效,因此无法实现底部对齐效果。下面提供两种常用实现方案:


方案1:底部布局随内容滚动(内容不足时贴屏幕底部,内容溢出时在内容末尾)

核心修改逻辑:利用LinearLayout的weight属性让内容区域占满剩余高度,自动把底部布局挤到最下方。
修改后的完整代码如下:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:fillViewport="true"
    android:paddingTop="65dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/backgroundmain"
    tools:context=".TeamInfoAll">

    <!-- 外层LinearLayout修改height为match_parent -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- 新增内容区域容器,设置weight=1占满剩余空间 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="10dp">

                <androidx.cardview.widget.CardView
                    android:id="@+id/topcard"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_rowWeight="1"
                    android:layout_columnWeight="1"
                    android:layout_marginLeft="14dp"
                    android:layout_marginRight="14dp"
                    android:layout_marginBottom="14dp"
                    app:cardBackgroundColor="#FAFAFA"
                    app:cardCornerRadius="12dp"
                    app:cardElevation="8dp"
                    tools:targetApi="lollipop">

                    <ImageView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/aboutbackground"
                        android:textAlignment="center"/>

                </androidx.cardview.widget.CardView>

            </LinearLayout>

            <GridLayout
                android:id="@+id/mainGrid"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:alignmentMode="alignMargins"
                android:columnOrderPreserved="false"
                android:padding="10dp"
                android:columnCount="1"
                android:rowCount="6">

                <!--Row 1-->
                <androidx.cardview.widget.CardView
                    android:id="@+id/cardView1"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    app:cardBackgroundColor="#4CAF50"
                    android:layout_columnWeight="1"
                    android:layout_rowWeight="1"
                    android:layout_marginBottom="14dp"
                    android:layout_marginLeft="14dp"
                    android:layout_marginRight="14dp"
                    app:cardElevation="8dp"
                    app:cardCornerRadius="12dp"
                    tools:targetApi="lollipop">

                    <TextView
                        android:id="@+id/cardView1Text"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="@string/Team1"
                        android:gravity="center_vertical"
                        android:textAlignment="center"
                        android:textColor="#FFFFFF"
                        android:textSize="25sp"
                        tools:ignore="RtlCompat" />
                </androidx.cardview.widget.CardView>

                <!--Row 2-->
                <androidx.cardview.widget.CardView
                    android:id="@+id/cardView2"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    app:cardBackgroundColor="#F44336"
                    android:layout_columnWeight="1"
                    android:layout_rowWeight="1"
                    android:layout_marginBottom="14dp"
                    android:layout_marginLeft="14dp"
                    android:layout_marginRight="14dp"
                    app:cardElevation="8dp"
                    app:cardCornerRadius="12dp"
                    tools:targetApi="lollipop">

                    <TextView
                        android:id="@+id/cardView2Text"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="@string/Team2"
                        android:gravity="center_vertical"
                        android:textAlignment="center"
                        android:textColor="#FFFFFF"
                        android:textSize="25sp"
                        tools:ignore="RtlCompat" />
                </androidx.cardview.widget.CardView>

                <!--Row 3-->
                <androidx.cardview.widget.CardView
                    android:id="@+id/cardView3"
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    app:cardBackgroundColor="#2196F3"
                    android:layout_columnWeight="1"
                    android:layout_rowWeight="1"
                    android:layout_marginBottom="14dp"
                    android:layout_marginLeft="14dp"
                    android:layout_marginRight="14dp"
                    app:cardElevation="8dp"
                    app:cardCornerRadius="12dp"
                    tools:targetApi="lollipop">

                    <TextView
                        android:id="@+id/cardView3Text"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="@string/Team3"
                        android:gravity="center_vertical"
                        android:textAlignment="center"
                        android:textColor="#FFFFFF"
                        android:textSize="22sp"
                        tools:ignore="RtlCompat" />
                </androidx.cardview.widget.CardView>

            </GridLayout>
        </LinearLayout>

        <!-- 底部布局保留原有配置即可 -->
        <LinearLayout
            android:id="@+id/banner_container1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:orientation="vertical"
            tools:ignore="ObsoleteLayoutParam" />

    </LinearLayout>
</ScrollView>

方案2:底部布局固定在屏幕底部,不跟随页面滚动

核心修改逻辑:根布局改为垂直LinearLayout,让ScrollView占满除底部布局外的所有屏幕空间。
修改后的核心结构如下:

<!-- 根布局改为垂直LinearLayout -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/backgroundmain"
    tools:context=".TeamInfoAll">

    <!-- ScrollView设置weight=1占满剩余高度 -->
    <ScrollView
        android:paddingTop="65dp"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fillViewport="true">

        <!-- 这里放你原来的所有内容,去掉底部的banner_container1 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <!-- 顶部卡片、GridLayout等原有内容 -->
        </LinearLayout>
    </ScrollView>

    <!-- 底部布局移到ScrollView外部,固定在屏幕底部 -->
    <LinearLayout
        android:id="@+id/banner_container1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:orientation="vertical"/>

</LinearLayout>

内容的提问来源于stack exchange,提问作者Imtiaz Shuvo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.09 07:45:00