Размещение рекламного баннера внизу родительского с фрагментами Android - PullRequest
0 голосов
/ 07 февраля 2020

Я делаю приложение android, но у меня проблемы с размещением баннера.

У меня есть действие, выполняющее роль контейнера для фрагментов. Я в основном хочу Scroll View для root, затем фрагмент и объявление в самом низу.

Вот мой XML.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"></LinearLayout>

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:layout_constraintBottom_toBottomOf="parent"
            ads:layout_constraintStart_toStartOf="parent"
            ads:layout_constraintEnd_toEndOf="parent"
            ads:layout_constraintTop_toBottomOf="@id/fragment_container"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-xxxx"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

Вот как выглядит макет как

enter image description here

По сути, объявление размещается непосредственно под контейнером фрагментов и не прикрепляется к нижней части области просмотра.

I хотите, чтобы он был прикреплен к нижней части, а затем прокрутите представление вместе с фрагментом + объявление вместе.

Это единственный способ показать объявление снизу (в краткосрочной перспективе):

<Constraint layout>
       <Scroll View>
          <Fragment container>
<Ad constraining to parent bottom>

Однако с помощью этого метода представление прокрутки будет только прокручивать фрагмент и не будет включать в себя рекламу, а это значит, что иногда вы будете получать объявление, перекрывающее содержание фрагмента.

Так что я думаю представление прокрутки должно быть представлением root. Однако у меня возникают проблемы с тем, чтобы дети показывали рекламу в нижней части окна с прокруткой вместо нижнего края детей.

Ответы [ 4 ]

1 голос
/ 07 февраля 2020

Вы можете сделать это, используя относительное расположение:

<?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:id="@+id/fragment_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" />


        </ScrollView>

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-xxxx"
            android:layout_alignParentBottom="true"/> <!--just add this line-->

    </RelativeLayout>
1 голос
/ 07 февраля 2020

Измените xml с кодом ниже

<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fillViewport="true"
        android:layout_weight="1">

        <androidx.appcompat.widget.LinearLayoutCompat
            android:id="@+id/fragment_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />

    </ScrollView>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-xxxx" />

</androidx.appcompat.widget.LinearLayoutCompat>

Пожалуйста, проверьте изображение ниже Это же, как вы хотите

enter image description here

Я надеюсь, что это может помочь вам!

Спасибо.

0 голосов
/ 07 февраля 2020

Привет Ницце! Можете ли вы попробовать это с RelativeLayout;)

<?xml version="1.0" encoding="utf-8"?>
        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:ads="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <LinearLayout
                    android:id="@+id/fragment_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" />


        </ScrollView>

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-xxxx"
            android:layout_alignParentBottom="true"/> <!--just add this line-->

    </RelativeLayout>
0 голосов
/ 07 февраля 2020

enter image description here

 <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            ads:layout_constraintTop_toTopOf="parent"
            ads:layout_constraintStart_toStartOf="parent"
            ads:layout_constraintEnd_toEndOf="parent"
            ads:layout_constraintBottom_toTopOf="@id/adView2">

            <LinearLayout
                android:id="@+id/fragment_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" />


        </ScrollView>

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="ca-app-pub-xxxx"
            ads:layout_constraintBottom_toBottomOf="parent"
            ads:layout_constraintEnd_toEndOf="parent"
            ads:layout_constraintStart_toStartOf="parent" />

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