【问题标题】:Toolbar fills the whole screen工具栏填满整个屏幕
【发布时间】:2016-12-12 19:21:55
【问题描述】:

我不熟悉抽屉布局和片段的使用。

我在使用DrawerLayout 创建活动时使用了tutorial。 它在getActionBar() 上给了我一个空指针,所以我将a toolbar 添加到活动主目录并改用getSupportActionBar()

现在看起来工具栏填满了整个屏幕,因为标题在屏幕中间,工具栏的背景也在整个屏幕上。这是xml代码:

<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">
    <!-- Toolbar -->
    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#FFFFFF" />
    <!-- The navigation drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#FFFFFF"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</android.support.v4.widget.DrawerLayout>

我尝试将 layout_height 替换为 56dp,并尝试使用其中的工具栏制作一个单独的布局文件并将其包含到此 xml 中,但这不起作用。

为什么我的工具栏会填满整个屏幕?

【问题讨论】:

  • 这个有什么价值?:android:layout_height="?attr/actionBarSize"
  • 那是 56dp。如果我删除它并乱扔布局_height =“56dp”,它仍然是全屏。
  • 将您的主题属性移动到高度属性之前(可能在那里设置高度)。

标签: android toolbar drawerlayout


【解决方案1】:

DrawerLayout 期望其中有 2 个视图。你提供 3 个。

第一个视图是“背景”。它是您屏幕的内容。第二个视图,抽屉,应该动画进出。

您提供 3 个视图。所以你的工具栏是“内容”——因此是全屏的,你的抽屉是你的实际内容。

要解决您的问题,请尝试以下操作:

<DrawerLayout>

  <!-- The main content view -->
  <LinearLayout>
    <Toolbar/>
    <FrameLayout
       android:id="@+id/content_frame"/>
  </LinearLayout>

  <!-- The navigation drawer -->
  <ListView/>
</DrawerLayout>

【讨论】:

  • 另外,android studio 中的预览存在一个错误。它向我显示了一个工具栏,其下方只有标题和我自己的工具栏。我已将工具栏设置为红色以使其位置更明显:imgur.com/a/qmc4G
  • 这不是错误,而是用于布局的主题。您可以更改预览中使用的主题以隐藏默认主题
【解决方案2】:

DrawerLayout 将只接受两个子视图。尝试在框架布局中添加工具栏。 或者你可以像下面这样创建。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <!-- The ActionBar -->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        app:layout_scrollFlags="scroll|enterAlways" />
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/flContent"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/white"
    app:headerLayout="@layout/layout_drawer_header"
    app:itemTextColor="#000000"
    app:menu="@menu/global">
</android.support.design.widget.NavigationView>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-06
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多