工具栏填满整个屏幕
Toolbar fills the whole screen
我是抽屉布局和片段使用的新手。
我用 tutorial 创建了 activity 和 DrawerLayout
。
它在 getActionBar()
上给了我一个空指针,所以我将 a toolbar 添加到 activity main 并改用 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>
我试过用 56dp 替换 layout_height,我试过制作一个单独的布局文件,其中包含工具栏并将其包含到这个 xml 中,但这没有用.
为什么我的工具栏会填满整个屏幕?
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>
A DrawerLayout
需要 2 个视图。你提供 3.
第一个视图是"background"。它是屏幕的内容。第二个视图,抽屉,应该有进出动画。
您提供了 3 个视图。所以你的工具栏是 "the content"——因此是全屏的,你的抽屉是你的实际内容。
要解决您的问题,请尝试以下操作:
<DrawerLayout>
<!-- The main content view -->
<LinearLayout>
<Toolbar/>
<FrameLayout
android:id="@+id/content_frame"/>
</LinearLayout>
<!-- The navigation drawer -->
<ListView/>
</DrawerLayout>
我是抽屉布局和片段使用的新手。
我用 tutorial 创建了 activity 和 DrawerLayout
。
它在 getActionBar()
上给了我一个空指针,所以我将 a toolbar 添加到 activity main 并改用 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>
我试过用 56dp 替换 layout_height,我试过制作一个单独的布局文件,其中包含工具栏并将其包含到这个 xml 中,但这没有用.
为什么我的工具栏会填满整个屏幕?
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>
A DrawerLayout
需要 2 个视图。你提供 3.
第一个视图是"background"。它是屏幕的内容。第二个视图,抽屉,应该有进出动画。
您提供了 3 个视图。所以你的工具栏是 "the content"——因此是全屏的,你的抽屉是你的实际内容。
要解决您的问题,请尝试以下操作:
<DrawerLayout>
<!-- The main content view -->
<LinearLayout>
<Toolbar/>
<FrameLayout
android:id="@+id/content_frame"/>
</LinearLayout>
<!-- The navigation drawer -->
<ListView/>
</DrawerLayout>