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

Android开发:AdView无法固定在屏幕底部显示的问题排查

Fixing AdMob AdView Fixed at Screen Bottom Issue

Hey there! I see you've been stuck on getting that AdView to stay fixed at the bottom no matter how long your text content is—let's sort this out quickly.

The Problem With Your Current Layout

Your root LinearLayout uses vertical orientation, and the ScrollView is set to wrap_content for its height. When your text is short, it fits neatly, so the AdView shows up below it. But when the text is long, the ScrollView expands to take up all remaining screen space (since wrap_content lets it grow with its content), pushing the AdView completely off the bottom of the screen.

Solution 1: Adjust LinearLayout Weights (Quick Fix for Your Existing Layout)

The simplest tweak is to change the ScrollView's height to 0dp and give it a layout_weight="1". This tells the LinearLayout to let the ScrollView fill up all the space between the top toolbar and the AdView, keeping the AdView pinned firmly at the bottom.

Here's your modified XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@color/backgroundColor" 
    tools:context=".activities.DetailActivity">

    <!-- Top Toolbar -->
    <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="50dp" 
        android:orientation="horizontal" 
        android:paddingLeft="16dp" 
        android:paddingRight="16dp" 
        android:elevation="1.46dp" 
        android:background="@color/backgroundColor">
        
        <TextView 
            android:id="@+id/tvWordDetail" 
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            android:layout_weight="1" 
            android:textColor="@color/textColorPrimary" 
            android:textSize="20sp" 
            android:gravity="center_vertical"/>
            
        <ImageButton 
            android:id="@+id/textToSpeech" 
            android:layout_width="48dp" 
            android:layout_height="48dp" 
            android:contentDescription="@string/text_to_speech" 
            android:background="?actionBarItemBackground" 
            app:srcCompat="@drawable/ic_volume"/>
            
        <ImageButton 
            android:id="@+id/btnBookmark" 
            android:layout_width="48dp" 
            android:layout_height="48dp" 
            android:contentDescription="@string/add_bookmark" 
            android:background="?actionBarItemBackground" 
            app:srcCompat="@drawable/ic_bookmark_border"/>
    </LinearLayout>

    <View 
        android:id="@+id/view" 
        android:layout_width="match_parent" 
        android:layout_height="1dp" 
        android:background="@color/viewColor"/>

    <!-- ScrollView: Now takes all available space except toolbar and AdView -->
    <ScrollView 
        android:id="@+id/scrollView" 
        android:layout_width="match_parent" 
        android:layout_height="0dp" <!-- Changed from wrap_content -->
        android:layout_weight="1"> <!-- Added this -->
        
        <LinearLayout 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:background="@color/backgroundColor" 
            android:elevation="1.46dp" 
            android:orientation="vertical" 
            android:paddingLeft="16dp" 
            android:paddingTop="10dp" 
            android:paddingRight="16dp">
            
            <TextView 
                android:id="@+id/tvWordDefinition" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:gravity="center_vertical" 
                android:textColor="@color/textColorSecondary" 
                android:textSize="18sp" />
        </LinearLayout>
    </ScrollView>

    <!-- AdView stays fixed at bottom -->
    <com.google.android.gms.ads.AdView 
        android:id="@+id/adViewDetail" 
        android:layout_width="match_parent" <!-- Changed to match_parent for full width -->
        android:layout_height="wrap_content" 
        ads:adSize="SMART_BANNER" 
        ads:adUnitId="@string/admob_banner_ad_detail">
    </com.google.android.gms.ads.AdView>
</LinearLayout>

I also adjusted the AdView's width to match_parent to ensure it fills the screen width properly—SMART_BANNER performs best with full-width placement.

Solution 2: Use ConstraintLayout (More Modern & Flexible)

If you're open to switching to ConstraintLayout (the recommended approach for most modern Android layouts), here's how to set it up to pin the AdView to the bottom:

<?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:ads="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/backgroundColor"
    tools:context=".activities.DetailActivity">

    <!-- Top Toolbar -->
    <LinearLayout
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:paddingLeft="16dp"
        android:paddingRight="16dp"
        android:elevation="1.46dp"
        android:background="@color/backgroundColor"
        app:layout_constraintTop_toTopOf="parent">
        
        <!-- Toolbar content remains unchanged -->
        <TextView 
            android:id="@+id/tvWordDetail" 
            android:layout_width="0dp" 
            android:layout_height="match_parent" 
            android:layout_weight="1" 
            android:textColor="@color/textColorPrimary" 
            android:textSize="20sp" 
            android:gravity="center_vertical"/>
            
        <ImageButton 
            android:id="@+id/textToSpeech" 
            android:layout_width="48dp" 
            android:layout_height="48dp" 
            android:contentDescription="@string/text_to_speech" 
            android:background="?actionBarItemBackground" 
            app:srcCompat="@drawable/ic_volume"/>
            
        <ImageButton 
            android:id="@+id/btnBookmark" 
            android:layout_width="48dp" 
            android:layout_height="48dp" 
            android:contentDescription="@string/add_bookmark" 
            android:background="?actionBarItemBackground" 
            app:srcCompat="@drawable/ic_bookmark_border"/>
    </LinearLayout>

    <View
        android:id="@+id/view"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/viewColor"
        app:layout_constraintTop_toBottomOf="@id/toolbar"/>

    <!-- ScrollView: Constrained between toolbar and AdView -->
    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@id/view"
        app:layout_constraintBottom_toTopOf="@id/adViewDetail">
        
        <LinearLayout 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content" 
            android:background="@color/backgroundColor" 
            android:elevation="1.46dp" 
            android:orientation="vertical" 
            android:paddingLeft="16dp" 
            android:paddingTop="10dp" 
            android:paddingRight="16dp">
            
            <TextView 
                android:id="@+id/tvWordDefinition" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:gravity="center_vertical" 
                android:textColor="@color/textColorSecondary" 
                android:textSize="18sp" />
        </LinearLayout>
    </ScrollView>

    <!-- AdView pinned to bottom of screen -->
    <com.google.android.gms.ads.AdView
        android:id="@+id/adViewDetail"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/admob_banner_ad_detail"
        app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

This setup gives you finer control over layout positions and scales better for complex UIs in the future.

Either solution will ensure your AdView stays fixed at the bottom, whether your text content is short or long!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:24:37