【问题标题】:Placing Android Banner Ads over ScrollView在 ScrollView 上放置 Android 横幅广告
【发布时间】:2020-06-24 12:14:44
【问题描述】:

我想在我的活动中放置两个横幅广告——一个在顶部,另一个在屏幕的底部。我有 LinearLayout 作为主要布局,它包含一个比屏幕大的 ScrollView。如果我将下面的 XML 代码放在 ScrollView 的末尾,它会离开屏幕。显然它需要绘制在 ScrollView 之上。我怎样才能做到这一点?

 <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"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="center"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView> 

【问题讨论】:

    标签: android xml android-layout layout banner-ads


    【解决方案1】:

    您可以通过将以下代码放入您的设计文件来实现此目的。

    更改您的 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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="DemoActivity">
    
    
        <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111"></com.google.android.gms.ads.AdView>
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"></ScrollView>
    
    
        <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"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
        </com.google.android.gms.ads.AdView>
    
    </LinearLayout>
    

    【讨论】:

    • 它有效。我是android开发的新手。我想解释一下滚动视图的高度和重量(您在此处使用的值)。
    • @Shuvo Sarker 每当我们需要将任何视图粘贴到顶部或底部并将其余高度分配给其他主视图时,那时我们将赋予权重属性,因此它将自动占据所有自己的休息空间。那时我们会将高度设置为“0”,因为当您设置权重时,该视图没有高度滚动。
    猜你喜欢
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 2021-02-26
    • 1970-01-01
    相关资源
    最近更新 更多