简体   繁体   English

如何以编程方式将横幅 Adview 放在底部?

[英]How can I put the banner Adview on bottom programmatically?

I'm trying to put the banners and interstitial from adMob to my app but the banner shows on top.我正在尝试将 adMob 中的横幅和插页式广告放到我的应用程序中,但横幅显示在顶部。 I want to put on bottom of screen programatically.我想以编程方式放在屏幕底部。 I have tried doing this in the xml file but stills showing on top.我曾尝试在 xml 文件中执行此操作,但仍然显示在顶部。

Java Class Java Class

    relativelayout = (RelativeLayout)findViewById(R.id.RelativeLayout);
        RelativeLayout.LayoutParams params = new    RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);

    AdView mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.BANNER);
    mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
    mAdView.setPadding(0,30, 0, 0);
    relativelayout.addView(mAdView, params);

XML XML

    <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:id="@+id/RelativeLayout"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/background_light"
tools:context=".MainActivity">

<TextView
    android:id="@+id/tvcifra"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/gridlayout"
    android:layout_alignTop="@id/tvcifra"
    android:layout_marginBottom="2dp"
    android:textColor="@android:color/holo_blue_light"
    android:textSize="48sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.gridlayout.widget.GridLayout
    android:id="@+id/gridlayout"
    android:layout_width="match_parent"
    android:layout_height="121dp"
    android:layout_above="@id/adView4"
    android:layout_alignParentTop="true"
    android:layout_marginTop="8dp"
    app:columnCount="6"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/tvcifra"
    app:orientation="vertical"
    app:rowCount="6">

    ...
    <button>1</button>
    <button>2</button>
    <button>3</button>
    <button>4</button>
    ...


</androidx.gridlayout.widget.GridLayout>

<com.google.android.gms.ads.AdView
    android:id="@+id/adView4"
    android:layout_width="660dp"
    android:layout_height="14dp"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-2703074771097768/1520884519"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/gridlayout" />

</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

Try adding this oh and don't forget to change ad unit id and also for width and height I don't know why you added some values but I would recommend keeping it as wrap_content尝试添加这个哦,不要忘记更改广告单元 ID 以及宽度和高度我不知道你为什么要添加一些值,但我建议将其保留为 wrap_content

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="420dp"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id6"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"/>

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

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