【问题标题】:Placing banner ad on bottom of parent with fragments Android将横幅广告放在带有片段Android的父级底部
【发布时间】:2020-02-07 05:08:36
【问题描述】:

我正在制作一个 Android 应用,但在定位横幅时遇到了问题。

我有一个 Activity 作为片段的容器。我基本上想要根的滚动视图,然后是片段,最底部的广告。

这是我的 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>

这是布局的样子

基本上,广告直接放置在片段容器的下方,而不是附加到视口的底部。

我希望它附加到底部,然后让滚动视图同时滚动片段和广告。

这是让广告出现在底部的唯一方法(短期内):

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

但是,使用这种方法,滚动视图只会滚动片段并且不包含广告,这意味着您有时会看到广告与片段内容重叠。

所以我认为滚动视图必须是根视图。但是,我无法管理孩子让广告出现在滚动视图的底部,而不是孩子的底部边缘。

【问题讨论】:

  • 可以使用相对布局吗?
  • @Karan - 将包含片段容器 + 广告的约束布局替换为相对布局会产生相同的结果。
  • 如果你想要@SmallGrammer,我可以使用相对布局添加答案
  • 这将有助于澄清。

标签: android admob fragment banner


【解决方案1】:

使用以下代码更改您的 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>

请检查下图是否与您想要的一样

希望对你有帮助!

谢谢。

【讨论】:

  • @SmallGrammer 你试试这个吗?
【解决方案2】:

你可以使用相对布局来做到这一点:

<?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>

【讨论】:

  • 感谢您的努力 :) 这就是为什么要投票!!
  • 哈哈!下次好运;)
【解决方案3】:

尼斯问候!你能用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>

【讨论】:

    【解决方案4】:

     <?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>
    

    【讨论】:

      猜你喜欢
      • 2014-11-23
      • 2020-06-24
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 2015-09-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      相关资源
      最近更新 更多