android - 状态栏下的 Material design 抽屉 : how to achieve it using a DrawerLayout with two FrameLayouts as children?

标签 android android-layout android-fragments navigation-drawer material-design

我目前正在设置抽屉导航以遵循 Material 设计规范。我已经检查过 Chris Banes post (在 Material design checklist 中可用)并制作了那里列出的所有实现,例如自定义主题等,以及 this question同样,但它们似乎都不符合我的应用程序结构的要求。

正如您从标题中猜到的那样,我的 DrawerLayout 有两个 FrameLayout 作为子级:一个用于内容,另一个用于抽屉(重要的是要指出,我无法更改此结构,因为我需要保持动态更新 NavigationDrawer 中的内容的能力,并且显然要保持交换 fragment 作为主要内容的能力)。

现在的问题是我不知道将工具栏声明放在 xml 中的什么位置才能实现预期的设计并且不会搞砸,因为 DrawerLayout 需要两个 child .

activity_main.xml

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.MainActivity" 
android:fitsSystemWindows="false"> <!-- Setting this one to true makes the status bar to have a non-transparent shitty background -->

<!--
     As the main content view, the view below consumes the entire
     space available using match_parent in both dimensions.
-->

<FrameLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" 
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />

    </FrameLayout>

<com.myapp.widget.ScrimInsetsFrameLayout
    android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navdrawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"/>
</android.support.v4.widget.DrawerLayout>

给出这个结果: 如您所见,状态栏似乎完全不存在(也许它是正确的?),因为内容是全屏绘制的。当应用程序启动时,我可以看到工具栏被绘制(呈现时没有状态栏填充,因此弄乱了系统图标)然后突然被 map 内容覆盖。我目前运行的是 Android 4.4.4,但它在 Android 5.0 的模拟器上是一样的。

App running on Android 4.4.4. CM11

在另一个 stackoverflow 问题中,建议将根 LinearLayout 与工具栏和 DrawerLayout 放在一起,但会导致奇怪的结果,例如正确呈现工具栏但下面是状态栏(带有纯色非透明背景)。我也尝试过使用 FrameLayout 作为 root,但结果很差。

关于如何处理这个问题有什么建议吗?

最佳答案

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"> 

<LinearLayout
    android:id="@+id/someLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:minHeight="?attr/actionBarSize"
        android:background="?attr/colorPrimary" 
        app:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
     <FrameLayout android:id="@+id/container"></FrameLayout>

</LinearLayout>

<com.myapp.widget.ScrimInsetsFrameLayout
    android:id="@+id/navigation_drawer"
    android:layout_width="@dimen/navdrawer_width"/>
</android.support.v4.widget.DrawerLayout>

现在您只需要更改R.id.container 中的 fragment ,因为容器在someLayout LinearLayout

中的ToolBar 下

我省略了一些布局参数,但这不是重点。

关于android - 状态栏下的 Material design 抽屉 : how to achieve it using a DrawerLayout with two FrameLayouts as children?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27471342/

相关文章:

一些小部件的Android支持包

Android - 下拉刷新整个页面

android - 检查 ExpandableListView 中的 Firstvisible item 是 Groupindicator?

android - FragmentTabHost & Fragments - 如何在选项卡之间传递数据?

android - 在 Fragment 中获取 not null Context 的保证方法是什么?

java - 如何从缓存共享 android gradle 库并从中构建

java - 从 onOptionsItemSelected 显示对话框

jquery - 有没有更好的Android webview

android - 我怎样才能在数字时钟中设置时间

android - 替换 fragment 会阻止抽屉的动画