【问题标题】:Expand Navigation Drawer Over Action Bar/Toolbar在操作栏/工具栏上展开导航抽屉
【发布时间】:2015-03-03 08:08:33
【问题描述】:

我一直在实现我的一个应用程序,并添加了 Lollipop 附带的 Navigation Drawer。阅读文档后,我发现我需要让抽屉覆盖操作栏。

搜索 Google 后,我找到了 this 帖子。问题是,我的抽屉设置不同,我想知道我是否需要重做它,或者我是否可以改变我已经完成的工作。

代码

ActivityDrawerLayout.java:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav_drawer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setBackgroundColor(Color.parseColor("#FFFFFF"));

        navigationDrawerItems = getResources().getStringArray(R.array.navigation_drawer_items);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        listView = (ListView) findViewById(R.id.left_drawer);

        // set a custom shadow that overlays the main content when the drawer opens
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        //drawerLayout.setStatusBarBackgroundColor(Color.parseColor("#ACACAC"));
        // set up the drawer's list view with items and click listener
        listView.setAdapter(new NavDrawerAdapter());
        listView.setOnItemClickListener(new DrawerItemClickListener());

        actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name);
        drawerLayout.setDrawerListener(actionBarDrawerToggle);

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        ...
    }

activity_nav_drawer.xml:

<LinearLayout
    android:id="@+id/main_parent_view"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    android:clipToPadding="false">

    <include layout="@layout/toolbar" />

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

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

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="304dp"
            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>
</LinearLayout>

注意 fitSystemWindows 属性已设置。

工具栏.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ToolbarCustomIconColor"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:fitsSystemWindows="true" />

Values-v21 主题:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppThemeNavDrawer" parent="Theme.AppCompat.NoActionBar">
        <item name="colorAccent">#F8F8F8</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    </style>
</resources>

所有这一切的最终结果是:

如您所见,它没有扩展。另外,我没有添加 statusBarColor,因为我的应用程序允许用户选择不同的颜色,所以它没有用。

感谢您的帮助!

【问题讨论】:

    标签: android


    【解决方案1】:

    我认为您需要修改 activity_nav_drawer.xml 布局。我建议如下:

    <android.support.v4.widget.DrawerLayout>
        <FrameLayout
            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
            <FrameLayout
                android:id="@+id/contentFrame"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </FrameLayout>
            <include layout="@layout/toolbar" />
        </FrameLayout>
        <include
                android:id="@+id/leftDrawer"
                android:layout_gravity="start"
                layout="@layout/navigation" />
    </android.support.v4.widget.DrawerLayout>
    

    【讨论】:

    • 这会是主要布局吗?
    • @AndrewQ 是的,activity_nav_drawer.xml
    • 嗯...好吧...让我做一些测试!
    • 其实……navigation布局是什么?
    • 你可以用你的left_drawer ListView替换它
    猜你喜欢
    • 2014-12-23
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 2019-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多