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

问题描述 投票:0回答:1

所以我一直试图在我的游戏屏幕顶部放置一个AdMob横幅(通过扩展的GLSurfaceView对象使用openGL进行渲染)。我正在使用一个RelativeLayout,我发现它在我的研究期间为其他人工作,当我将它对齐到底部viaadParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)时它适用于我:

enter image description here

然而,当改变为adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP)时,我遇到了问题:

enter image description here

它确实将广告放在顶部,但屏幕的整个底部是白色的,我的表面视图不再可见。它为什么这样做?我已经尝试了一堆不同的布局和我发现的其他建议,但似乎没有任何效果。

我能够让广告只占用屏幕的一小部分,使用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);
    }
}
java android android-layout admob opengl-es-2.0
1个回答
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" />

© www.soinside.com 2019 - 2024. All rights reserved.