如何在Android Studio中将AdView和NestedScrollView分离并防止它们重叠?

huangapple go评论142阅读模式
英文:

How do I seperate AdView and NestedScrollView from each other and stop them from overlaping in android studio?

问题

我试图在Android Studio中创建一个用于我的adView横幅广告的空间,但adView重叠在我的widget NestedScrollView上,并覆盖了位于NestedScrollView底部的文字。我的XML代码都包含在一个ConstraintLayout小部件中。如何分离adView和NestedScrollView小部件,使它们不重叠在一起?这是我的Android Studio activity_main.xml页面的当前格式:

<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/grey"
    tools:context=".MainActivity">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/ScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </androidx.core.widget.NestedScrollView>

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">
    </com.google.android.gms.ads.AdView>

</androidx.constraintlayout.widget.ConstraintLayout>
英文:

I am trying to create a space for my adView banner ad in android studio but the adView is overlapping my widget NestedScrollView, and is covering the words at the bottom which are in the NestedScrollView. All of my xml code is encased in a widget ConstraintLayout. How do I seperate the adView and NestedScrollView widget from overlapping one another? This is the current format of my android studio activity_main.xml page

&lt;androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
    xmlns:tools=&quot;http://schemas.android.com/tools&quot;
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;match_parent&quot;
    android:orientation=&quot;vertical&quot;
    android:background=&quot;@color/grey&quot;
    tools:context=&quot;.MainActivity&quot;&gt;


    &lt;androidx.core.widget.NestedScrollView
        android:id=&quot;@+id/ScrollView&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;match_parent&quot;&gt;

&lt;/androidx.core.widget.NestedScrollView&gt;
    &lt;com.google.android.gms.ads.AdView
        xmlns:ads=&quot;http://schemas.android.com/apk/res-auto&quot;
        android:id=&quot;@+id/adView&quot;
        android:layout_width=&quot;wrap_content&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:layout_centerHorizontal=&quot;true&quot;
        android:layout_alignParentBottom=&quot;true&quot;
        ads:adSize=&quot;BANNER&quot;
        ads:adUnitId=&quot;ca-app-pub-3940256099942544/6300978111&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:layout_constraintEnd_toEndOf=&quot;parent&quot;
        app:layout_constraintStart_toStartOf=&quot;parent&quot;&gt;
    &lt;/com.google.android.gms.ads.AdView&gt;


&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案1

得分: 1

Your NestedScrollView的高度设置为match_parent,这使它与ConstraintSet的大小相同。
如果将其设置为0dp并将顶部约束到父项的顶部和底部约束到adView的顶部,它将与两者分开布局。

    <androidx.core.widget.NestedScrollView
        android:id="@+id/ScrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"  
        app:layout_constraintBottom_toTopOf="@id/adView"  >
英文:

Your NestedScrollView's height is set to match_parent this makes it the size of the ConstraintSet.
If you set it to 0dp and constrain top to top of the parent and bottom to top of the
adView it would layout with the two separated.

    &lt;androidx.core.widget.NestedScrollView
        android:id=&quot;@+id/ScrollView&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;0dp&quot;
        app:layout_constraintTop_toTopOf=&quot;parent&quot;  
        app:layout_constraintBottom_toTopOf=&quot;@id/adView&quot;  &gt;

huangapple
  • 本文由 发表于 2023年2月6日 06:45:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356023.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定