Как правильно разместить баннер AdMob в верхней части GLSurfaceView - PullRequest
0 голосов
/ 15 марта 2019

Поэтому я пытался разместить баннер AdMob в верхней части игрового экрана (который отображается с использованием openGL через расширенный объект GLSurfaceView).Я использую RelativeLayout, который, как я обнаружил, работал для других людей во время моего исследования, и он работает для меня, когда я выравниваю его по дну с помощью adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM):

enter image description here

однако, когда это изменяется на adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP), я сталкиваюсь с проблемами:

enter image description here

Это действительно помещает объявление всверху, но вся нижняя часть экрана белая, и мой вид с поверхности больше не виден.Почему он это делает?Я пробовал кучу разных макетов и других предложений, которые нашел вокруг, но, похоже, ничего не работает.

Мне удалось заставить объявление занимать только небольшую часть экрана с помощью ConstraintLayout и размещения LinearLayout / RelativeLayout, к которому я добавил свой GLSurfaceView, но оно не отображалось.Также кажется, что 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 голосов
/ 15 марта 2019

Почему вы создаете свой вид во время выполнения? просто создайте его в своем 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" />

...