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

Android Studio中如何在ScrollView外添加不受滚动影响的AdView

解决ScrollView底部固定横幅广告的问题

你的问题出在把AdView放在了ScrollView内部的LinearLayout里,所以广告会跟着ScrollView一起滚动。要让广告固定在屏幕底部不受滚动影响,必须把AdView移到ScrollView外面,用根布局来控制两者的位置。

推荐用ConstraintLayout作为根布局,这是Android中最灵活的布局方式,下面是修改后的完整activity_main.xml代码:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/grey"
    tools:context=".MainActivity">

    <!-- 滚动内容区域 -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/adContainer"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.cardview.widget.CardView
                android:layout_margin="16dp"
                app:cardCornerRadius="8dp"
                app:cardElevation="8dp"
                app:cardBackgroundColor="@color/blue"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <androidx.appcompat.widget.SearchView
                    android:id="@+id/search_view"
                    app:defaultQueryHint="Search..."
                    app:iconifiedByDefault="false"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"/>
            </androidx.cardview.widget.CardView>

            <LinearLayout
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/textView_a"
                    android:textSize="24sp"
                    android:text="A"
                    android:textColor="@color/blue"
                    android:textAlignment="center"
                    android:padding="8dp"
                    android:textStyle="italic"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recycler_a"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <TextView
                    android:textSize="24sp"
                    android:text="B"
                    android:textColor="@color/blue"
                    android:textAlignment="center"
                    android:padding="8dp"
                    android:textStyle="italic"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recycler_b"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
            </LinearLayout>

        </LinearLayout>
    </ScrollView>

    <!-- 底部固定广告容器 -->
    <LinearLayout
        android:id="@+id/adContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent">

        <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="SMART_BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111"/>
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

关键修改点:

  1. 根布局换成ConstraintLayout,用来精确控制ScrollView和广告的位置
  2. ScrollView的android:layout_height设为0dp,通过约束让它占满屏幕顶部到广告顶部的所有空间,避免滚动内容被广告遮挡
  3. 广告容器adContainer通过app:layout_constraintBottom_toBottomOf="parent"固定在屏幕底部,同时设置左右对齐父布局
  4. 把原来ScrollView内部的广告代码完全移到外面,确保它不属于滚动内容的一部分

如果习惯用RelativeLayout,也可以用下面的写法:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    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:background="@color/grey"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/adContainer">

        <!-- 这里放原来的滚动内容,和之前的代码一致 -->
    </ScrollView>

    <LinearLayout
        android:id="@+id/adContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="vertical"
        android:layout_alignParentBottom="true">

        <!-- 广告代码 -->
    </LinearLayout>

</RelativeLayout>

两种方式都能实现广告固定在底部、不受ScrollView滚动影响的效果,选你熟悉的布局方式即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 07:50:28