简体   繁体   English

ScrollView 不会停留在屏幕的某个部分并占据整个屏幕

[英]ScrollView doesn't stay to a certain part of the screen and takes whole screen

I am trying to limit my ScrollView to only a certain section of my screen as I am trying to attach a banner ad to the bottom of my screen and I want the banner ad to be static while the rest of the contents to be scroll-able therefore I don't want the banner ad to be affected by the ScrollView.我试图将 ScrollView 限制在屏幕的特定部分,因为我试图将横幅广告附加到屏幕底部,我希望横幅广告为 static,而内容的 rest 可滚动因此我不希望横幅广告受到 ScrollView 的影响。 For some reason though only the scroll-able section is visible on my device screen when I run my app, my banner ad does not show up at all.出于某种原因,虽然当我运行我的应用程序时我的设备屏幕上只能看到可滚动部分,但我的横幅广告根本没有显示。 I have tried multiple methods to fix this problem to make it work and yet I still can't figure it out.我已经尝试了多种方法来解决这个问题以使其正常工作,但我仍然无法弄清楚。 This is my current activity_main.xml code这是我当前的 activity_main.xml 代码

<?xml version="1.0" encoding="utf-8"?>
<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">


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.cardview.widget.CardView
            android:layout_margin="16dp"
            app:cardCornerRadius="8dp"
            app:cardElevation="8dp"
            app:cardBackgroundColor="@color/blue"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <androidx.appcompat.widget.SearchView
                android:id="@+id/search_view"
                app:defaultQueryHint="Search..."
                app:iconifiedByDefault="false"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"/>
        </androidx.cardview.widget.CardView>

        <LinearLayout
            android:layout_margin="16dp"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/textView_a"
                android:textSize="24sp"
                android:text="A"
                android:textColor="@color/blue"
                android:textAlignment="center"
                android:padding="8dp"
                android:textStyle="italic"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_phonetics"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <TextView
                android:textSize="24sp"
                android:text="B"
                android:textColor="@color/blue"
                android:textAlignment="center"
                android:padding="8dp"
                android:textStyle="italic"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_meanings"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent">
    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        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>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

First of all, never use RecyclerView inside Scrollview .首先,永远不要Scrollview中使用RecyclerView Instead, use NestedScrollView .相反,使用NestedScrollView

NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default. NestedScrollView 和ScrollView 一样,但是它支持在Android 的新旧版本上同时充当嵌套滚动父子视图。嵌套滚动是默认启用的。

If you don't want your RecyclerViews too be scrollable in the first place, add your content as child views to a layout view.如果您首先不希望RecyclerViews太可滚动,请将您的内容作为子视图添加到布局视图中。

Second, for your ScrollView to take up all the remaining space given by the AdView , craft a constraint chain and give the ScrollView a height of 0dp .其次,为了让您的ScrollView占据AdView给定的所有剩余空间,制作一个约束链并为ScrollView指定0dp的高度。 This means, you need to constrain the bottom of the ScrollView to the top of the AdView and vice versa.这意味着,您需要将ScrollView的底部限制在AdView的顶部,反之亦然。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
 ...>
    <android.support.v4.widget.NestedScrollView 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@+id/ScrollView"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@id/LinearAdLayout">

        <!--Your content here-->

    </android.support.v4.widget.NestedScrollView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LinearAdLayout"
        app:layout_constraintTop_toBottomOf="@id/ScrollView"
        app:layout_constraintBottom_toBottomOf="parent">
        
        <!--Your AdView here-->

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM