如何使用DrawerLayout在操作栏/工具栏上方和状态栏下方显示?

本文翻译自:How do I use DrawerLayout to display over the ActionBar/Toolbar and under the status bar?

I've seen in the new material design Side Nav spec that you can display the drawer over the action bar and behind the status bar. 我在新的材质设计Side Nav规范中看到,可以在操作栏上方和状态栏后面显示抽屉。 How can I implement this? 我该如何实施?


#1楼

参考:https://stackoom.com/question/1mwTn/如何使用DrawerLayout在操作栏-工具栏上方和状态栏下方显示


#2楼

New functionality in the framework and support libs allow exactly this. 框架和支持库中的新功能完全可以做到这一点。 There are three 'pieces of the puzzle': 一共有三个“谜题”:

  1. Using Toolbar so that you can embed your action bar into your view hierarchy. 使用工具栏,以便您可以将操作栏嵌入到视图层次结构中。
  2. Making DrawerLayout fitsSystemWindows so that it is layed out behind the system bars. 使DrawerLayout适合 fitsSystemWindows以便将其fitsSystemWindows在系统栏后面。
  3. Disabling Theme.Material 's normal status bar coloring so that DrawerLayout can draw there instead. 禁用Theme.Material的正常状态栏着色,以便DrawerLayout可以在此处绘制。

I'll assume that you will use the new appcompat. 我假设您将使用新的appcompat。

First, your layout should look like this: 首先,您的布局应如下所示:

<!-- The important thing to note here is the added fitSystemWindows -->
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- Your normal content view -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- We use a Toolbar so that our drawer can be displayed
             in front of the action bar -->
        <android.support.v7.widget.Toolbar  
            android:id="@+id/my_awesome_toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary" />

        <!-- The rest of your content view -->

    </LinearLayout>

    <!-- Your drawer view. This can be any view, LinearLayout
         is just an example. As we have set fitSystemWindows=true
         this will be displayed under the status bar. -->
    <LinearLayout
        android:layout_width="304dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:fitsSystemWindows="true">

        <!-- Your drawer content -->

    </LinearLayout>

</android.support.v4.widget.DrawerLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 DrawerLayout 中添加多个侧滑,可以使用一个布局来作为容器,然后在该容器中添加多个 Fragment。以下是一种实现方式: 1. 在布局文件中创建一个 DrawerLayout,并且在其中添加一个 FrameLayout 作为侧滑容器。 ```xml <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 主布局 --> <FrameLayout android:id="@+id/main_content" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- 侧滑容器 --> <FrameLayout android:id="@+id/drawer_container" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" /> </android.support.v4.widget.DrawerLayout> ``` 2. 创建多个侧滑 Fragment,并且分别添加到侧滑容器中。 ```java // 添加第一个侧滑 FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.drawer_container, new FirstDrawerFragment()); transaction.commit(); // 添加第二个侧滑 transaction = getSupportFragmentManager().beginTransaction(); transaction.add(R.id.drawer_container, new SecondDrawerFragment()); transaction.commit(); ``` 3. 在主布局中添加菜单按钮,点击菜单按钮时切换侧滑显示和隐藏状态。 ```java ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawerLayout.addDrawerListener(toggle); toggle.syncState(); // 切换侧滑显示和隐藏状态 navigationView.setNavigationItemSelectedListener(item -> { drawerLayout.closeDrawer(GravityCompat.START); return true; }); ``` 这样就可以实现 DrawerLayout 中添加多个侧滑的功能了。需要注意的是,多个侧滑之间可能会有冲突,需要按照需求进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值