Jan*_*n96 3 android toolbar drawerlayout
我是抽屉布局和片段使用的新手。
我使用的教程上与创建活动DrawerLayout。它给了我一个空指针getActionBar(),所以我在活动 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>
Run Code Online (Sandbox Code Playgroud)
我试过用 56dp 替换 layout_height,我试过用工具栏制作一个单独的布局文件,并将它包含到这个 xml 中,但这没有用。
为什么我的工具栏会填满整个屏幕?
ADrawerLayout期望其中有2 个视图。你提供 3。
第一个视图是“背景”。它是您屏幕的内容。第二个视图,抽屉,应该动画进出。
您提供 3 个视图。所以你的工具栏是“内容”——因此是全屏的,你的抽屉是你的实际内容。
要解决您的问题,请尝试以下操作:
<DrawerLayout>
<!-- The main content view -->
<LinearLayout>
<Toolbar/>
<FrameLayout
android:id="@+id/content_frame"/>
</LinearLayout>
<!-- The navigation drawer -->
<ListView/>
</DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1932 次 |
| 最近记录: |