为什么滚动时我的应用栏与屏幕上的项目重叠?

问题描述 投票:1回答:1

就像我在问题中描述的那样,当我运行该应用程序并向下滚动内容时,它只会越过顶部栏,直到手机通知面板的下边缘。我也无法单击左侧的3行图标来打开侧面导航抽屉。而是我必须将其从左侧拖动才能打开它,就像我的顶部栏甚至没有被注意到一样。我正在使用android studio中的应用程序。

以下是我的包含它的XML页面的代码:

活动首页:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <androidx.viewpager.widget.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <include
        layout="@layout/app_bar_front_page"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="?android:attr/actionBarSize">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="?android:attr/actionBarSize">

        <androidx.cardview.widget.CardView

            android:layout_width="275dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="50dp"
            app:cardBackgroundColor="#3B1187"
            app:cardCornerRadius="25dp"
            app:cardElevation="10dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="107dp"
                android:orientation="horizontal"
                android:padding="15dp">

                <TextView
                    android:id="@+id/news_box"
                    android:layout_width="260dp"
                    android:layout_height="84dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:fontFamily="cursive"
                    android:gravity="start"
                    android:onClick="onClickNews"
                    android:text="News"
                    android:textColor="#FFFFFF"
                    android:textSize="35dp" />

                <ImageView
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="7dp"
                    android:layout_marginEnd="23dp"
                    android:src="@drawable/news_icon" />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="277dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_marginBottom="50dp"
            app:cardBackgroundColor="#3B1187"
            app:cardCornerRadius="25dp"
            app:cardElevation="10dp">

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

                <TextView
                    android:id="@+id/fact"
                    android:layout_width="259dp"
                    android:layout_height="78dp"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentBottom="true"
                    android:layout_marginStart="14dp"
                    android:layout_marginBottom="3dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:fontFamily="cursive"
                    android:gravity="start"
                    android:onClick="onClickFacts"
                    android:text="Facts"
                    android:textColor="#FFFFFF"
                    android:textSize="35dp" />

                <ImageView
                    android:id="@+id/imageView2"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="13dp"
                    android:src="@drawable/real_facts_icon" />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>

        <androidx.cardview.widget.CardView
            android:layout_width="286dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:paddingTop="50dp"
            app:cardBackgroundColor="#3B1187"
            app:cardCornerRadius="25dp"
            app:cardElevation="10dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="89dp">

                <ImageView
                    android:id="@+id/imageView3"
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:layout_alignParentTop="true"
                    android:layout_alignParentEnd="true"
                    android:layout_marginTop="8dp"
                    android:layout_marginEnd="11dp"
                    android:src="@drawable/steps" />

                <TextView
                    android:id="@+id/step"
                    android:layout_width="274dp"
                    android:layout_height="77dp"
                    android:layout_alignParentStart="true"
                    android:layout_marginStart="6dp"
                    android:layout_marginBottom="155dp"
                    android:clickable="true"
                    android:focusable="true"
                    android:fontFamily="cursive"
                    android:gravity="start"
                    android:onClick="onClickSteps"
                    android:text="Steps"
                    android:textColor="#FFFFFF"
                    android:textSize="35dp" />
            </RelativeLayout>
        </androidx.cardview.widget.CardView>
        //newsletteretc
        <GridLayout
            android:paddingTop="20dp"
            android:columnCount="2"
            android:rowCount="1"
            android:alignmentMode="alignMargins"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <androidx.cardview.widget.CardView
                android:layout_width="200dp"
                android:layout_height="200dp"
                app:cardCornerRadius="20dp"
                app:cardElevation="10dp"
                android:layout_marginBottom="50dp"
                android:background="@drawable/newsletter"
                android:layout_margin="12dp">
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/bg"
                    android:layout_centerInParent="true"
                    android:paddingRight="20dp">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="Newsletter"
                        android:id="@+id/newtext"
                        android:textSize="25dp"
                        android:gravity="center"
                        />
                </RelativeLayout>

            </androidx.cardview.widget.CardView>
            <androidx.cardview.widget.CardView
                android:layout_width="200dp"
                android:layout_height="200dp"
                app:cardCornerRadius="20dp"
                app:cardElevation="10dp"
                android:layout_marginBottom="50dp"
                android:layout_margin="12dp">
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/ad"
                    android:layout_centerInParent="true"
                    android:paddingRight="100dp">
                </RelativeLayout>
            </androidx.cardview.widget.CardView>
        </GridLayout>
        //share
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <androidx.cardview.widget.CardView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:cardCornerRadius="20dp"
                app:cardElevation="10dp"
                android:layout_margin="12dp"
                app:cardBackgroundColor="#00FFB7">

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:columnCount="3"
                    android:orientation="vertical"
                    android:rowCount="1"
                    android:textAlignment="center">

                    <ImageButton
                        android:id="@+id/facebook"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        app:srcCompat="@drawable/fb_new" />

                    <ImageButton
                        android:id="@+id/imageButton2"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:layout_toRightOf="@id/facebook"
                        app:srcCompat="@drawable/insta_new" />

                    <ImageButton
                        android:id="@+id/imageButton3"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        app:srcCompat="@drawable/wp"
                        android:layout_toRightOf="@id/imageButton2"/>

                </RelativeLayout>

            </androidx.cardview.widget.CardView>
        </RelativeLayout>

        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_front_page"
        app:menu="@menu/activity_front_page_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

另一个用于应用程序栏的XML文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    tools:context=".Front_page">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_front_page" />

</androidx.coordinatorlayout.widget.CoordinatorLayout> 

注意:在创建新活动时,我选择了“导航抽屉”活动,并发现其中有一个XML特别是用于顶部应用程序/工具栏。

滚动前:image before scrolling

滚动后:Image after scrolling

我希望项目在顶部栏下滚动。

Edit:下面是代码的更改部分,这也导致内容在页面开始时与应用程序栏重叠显示,并在滚动过程中继续重叠:

<include
        layout="@layout/app_bar_front_page"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <androidx.viewpager.widget.ViewPager
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_behavior ="@string/appbar_scrolling_view_behavior"
        android:layout_below="@+id/toolbar">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
java android xml android-studio android-toolbar
1个回答
0
投票

@ cgb_pandey指出的解决方案是将滚动视图及其子代代码简单地移到content_front_page.xml

© www.soinside.com 2019 - 2024. All rights reserved.