尝试将导航抽屉按钮添加到应用栏

时间:2018-07-30 08:51:02

标签: java android xml android-studio-3.0

我正在关注this tutorial

我被困在试图将按钮添加到应用栏的位置。

当我向右滑动时,会出现导航抽屉,这样就可以了。它只是我遇到问题的栏中的按钮,当我将v7.widget.Toolbar添加到FrameLayout下的XML中时,它会破坏布局。

最初,在我的XML中,我只有一个约束布局,其中Scrollview作为子级,而在其中则是LinearView(垂直)。

但是我无法理解为什么它不起作用:

<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">


    <!-- Container for contents of drawer - use NavigationView to make configuration easier -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer_view"
        app:headerLayout="@layout/nav_header" />

    <!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

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



            <LinearLayout
                android:id="@+id/seeds_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="16dp"
                android:background="@drawable/customborder"
                android:orientation="vertical"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"></LinearLayout>
        </ScrollView>

    </FrameLayout>

运行此命令时,顶部栏似乎超过了整个窗口,并且颜色消失了,并且按钮不可单击,因为LinearLayout位于顶部。我要去哪里错了?

2 个答案:

答案 0 :(得分:1)

NavigationView放在FrameLayout下方。

答案 1 :(得分:1)

在上面MikeM的评论之后,他说:

FrameLayout将重叠其子视图;也就是说,它将在z轴上一个一个地堆叠它们。将FrameLayout更改为垂直的LinearLayout,以将它们逐边布置。另外,您的NavigationView必须在DrawerLayout中列在最后。

所以基本上我已经将FrameLayout更改为Linearlayout,并且可以正常工作。令人失望的是,Android文档就是这样,但我该怎么做。

谢谢MikeM!

相关问题