【问题标题】:Trying to add nav drawer button to the app bar尝试将导航抽屉按钮添加到应用栏
【发布时间】:2018-07-30 08:51:01
【问题描述】:

我关注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 在它的顶部。我哪里错了?

【问题讨论】:

  • FrameLayout 将与其子 Views 重叠;即,它将在 z 轴上将它们一个叠放在另一个之上。将FrameLayout 更改为垂直LinearLayout,这将使它们边对边排列。此外,您的NavigationView 需要在DrawerLayout 中最后列出。
  • @MikeM。我不明白为什么他们会在教程中指定使用框架布局?这也行得通!但是我已经失去了工具栏的颜色,我猜是因为我现在正在使用自己的工具栏。
  • @MikeM。如果你把它作为答案,我会把它标记为正确的。
  • 是的,他们搞砸了那个例子。可悲的是,这并不奇怪。不管怎样,我很好。 :-) 这是一个常见问题,如果我有时间去寻找一个,我可能会把它作为一个副本关闭。如果您愿意,请随时在您自己的答案中发布调整后的布局。不过谢谢。我很感激这个提议。顺便说一句,是的,您需要自己设置Toolbar 的背景;例如,android:background="?attr/colorPrimary"。干杯!

标签: java android xml android-studio-3.0


【解决方案1】:

NavigationView 放在FrameLayout 下方。

【讨论】:

  • 谢谢,但这并没有解决问题。
【解决方案2】:

继上面 MikeM 的 cmets 之后,他说:

FrameLayout 会覆盖它的子 View;即,它将在 z 轴上将它们一个叠放在另一个之上。将 FrameLayout 更改为垂直 LinearLayout,这将使它们边对边地布置。此外,您的 NavigationView 需要在 DrawerLayout 中最后列出。

所以基本上我已经将我的 FrameLayout 更改为 Linearlayout 并且有效。有点令人失望的是,Android 文档是这样的,但我是这样做的。

谢谢,MikeM!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多