如何将AdMob横幅正确放置在GLSurfaceView的顶部

时间:2019-03-15 01:53:30

标签: java android android-layout admob opengl-es-2.0

因此,我一直试图在广告屏幕的顶部放置一个AdMob横幅广告(通过扩展的GLSurfaceView对象使用openGL呈现)。我使用的是RelativeLayout,在研究期间我发现它在为其他人工作,当我通过adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)将它与底部对齐时,它对我有用:

enter image description here

无论何时将其更改为adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP),我都会遇到问题:

enter image description here

它确实将广告放置在顶部,但屏幕的整个底部为白色,并且我的Surfaceview不再可见。为什么这样做呢?我尝试了很多不同的布局和其他建议,但似乎没有任何效果。

使用ConstraintLayout并放置我向其添加了GLSurfaceView的LinearLayout / RelativeLayout,我只能使广告仅占据屏幕的一小部分,但并未显示。似乎还有SurfaceView,因为我在触摸事件时可以听到音乐过渡和正在玩的游戏。

以下是我目前正在执行的代码:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    //set fullscreen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                         WindowManager.LayoutParams.FLAG_FULLSCREEN);

    //create our rendering surface
    mGLView = new GameSurfaceView(this, this);
    mGLView.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);

    //premium players don't get ads
    if(is_premium)
    {
        setContentView(mGLView);
    }
    else
    {
        MobileAds.initialize(this, ad_app_id);

        //initialize interstitial ad
        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId(ad_string_inter_test);
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        mInterstitialAd.setAdListener(new AdListener()
        {
            @Override
            public void onAdClosed()
            {
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            }
        });

        //create our layout and add our openGL scene
        layout = new RelativeLayout(this);
        layout.setLayoutParams(new WindowManager.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT));
        layout.addView(mGLView);
        setContentView(layout);

        //initialize banner ad
        mBannerAd = new AdView(this);
        mBannerAd.setAdSize(AdSize.SMART_BANNER);
        mBannerAd.setAdUnitId(ad_string_banner_test);
        mBannerAd.setBackgroundColor(Color.TRANSPARENT);
        mBannerAd.setAdListener(new AdListener()
        {
            @Override
            public void onAdLoaded()
            {
                RelativeLayout.LayoutParams adParams =
                    new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                                                    RelativeLayout.LayoutParams.WRAP_CONTENT);

                adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
                adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                //adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);

                layout.addView(mBannerAd, adParams);
            }
        });


        //create ad request and begin loading
        AdRequest bannerRequest = new AdRequest.Builder().addTestDevice(DEVICE_TEST_ID).build();
        mBannerAd.loadAd(bannerRequest);
    }
}

1 个答案:

答案 0 :(得分:0)

为什么要在运行时创建视图?只需在XML中创建它,最佳的响应式布局就是constraintLayout,下面是一个示例:

<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/frameLayout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragments.MenusDesign.ExpandableCategoriesMenu.ExpandableCategoriesMenu"
tools:layout_editor_absoluteY="81dp">


<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:text="Soduku"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline2" />

<Button
    android:id="@+id/button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="Ad mob  view"
    app:layout_constraintBottom_toTopOf="@+id/guideline2"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.1"/>

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.4" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.55" />

<androidx.constraintlayout.widget.Guideline
    android:id="@+id/guideline4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    app:layout_constraintGuide_percent="0.7" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="Easy"
    app:layout_constraintBottom_toTopOf="@+id/guideline3"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView4" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="Medium"
    app:layout_constraintBottom_toTopOf="@+id/guideline5"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline3" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="hard"
    app:layout_constraintBottom_toTopOf="@+id/guideline4"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline5" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    android:text="Exit"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline4" />