简体   繁体   English

如何以编程方式在屏幕底部显示 AdMob 横幅广告?(Kotlin)

[英]How to display AdMob banner ad at the bottom of the screen programmatically?(Kotlin)

I'm new to Kotlin and Android.我是 Kotlin 和 Android 的新手。 I want to display an AdMob banner ad at the bottom of the screen programmatically.我想以编程方式在屏幕底部显示 AdMob 横幅广告。 But I can only display it at the top of the screen.但我只能在屏幕顶部显示它。 Here is my code:这是我的代码:

activity_main.xml:活动_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:id="@+id/activityMain"
    tools:context=".MainActivity">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="false"
        android:scaleType="centerCrop"
        android:id="@+id/rootImageView"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:id="@+id/testTextView"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt:主活动.kt:

import android.app.Notification
import android.content.res.Configuration
import android.graphics.Color
import android.media.AudioManager
import android.media.MediaPlayer
import android.media.ToneGenerator
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.constraintlayout.widget.ConstraintLayout
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.AdSize
import com.google.android.gms.ads.AdView
import com.google.android.gms.ads.MobileAds
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if(useAdMob){ initAdMob(); }
    }
    //---------------------------------------------
    fun initAdMob()
    {
        MobileAds.initialize(this);

        val adView = AdView(this);
        adView.adSize = AdSize.BANNER;
        adView.adUnitId = if(testAdMob) idAdMobBannerForTest else idAdMobBanner1;
        val adRequest = AdRequest.Builder().build();
        adView.loadAd(adRequest);

        activityMain.addView(adView);
        adView.setBackgroundColor(Color.RED);
        
        val lp = adView.layoutParams;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        println(">>>> adView, width: ${lp.width}, height: ${lp.height}");
    }
}

I've searched a lot for the solution, for example, try to:我已经搜索了很多解决方案,例如,尝试:

addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)

or add in xml:或在 xml 中添加:

android:layout_alignParentBottom="true"

But these seems unavailable in the newest version of Android Studio.但是这些在最新版本的 Android Studio 中似乎不可用。 Please help me.请帮我。 Thanks in advance.提前致谢。

I would advise not doing this programatically, but you can use ConstraintSet to set Constraint layout rules programatically.我建议不要以编程方式执行此操作,但您可以使用ConstraintSet以编程方式设置约束布局规则。 Looks something like this:看起来像这样:

adView.id = View.generateViewId() // Need to give view an id

ConstraintSet().apply {
    connect(adView.id, ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM)
    applyTo(constraintLayout) // Your constraint layout here
}

This is equivalent to in xml doing这相当于在 xml 中做的

app:layout_constraintBottom_toBottomOf="parent"

Since you use constraint layout try this below code...由于您使用约束布局,请尝试使用以下代码...

<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:id="@+id/bannerAd"
   ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
   app:layout_constraintBottom_toBottomOf="parent"
   app:layout_constraintStart_toStartOf="parent"
   app:layout_constraintEnd_toEndOf="parent"/>

and in kotlin file并在 kotlin 文件中

MobileAds.initialize(this) {}
    var mAdView = findViewById(R.id.bannerAd)
    val adRequest = AdRequest.Builder().build()
    mAdView.loadAd(adRequest)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM