在操作栏/工具栏上展开导航抽屉

时间:2015-03-03 08:08:33

标签: android

我一直在实现我的一个应用程序,并添加了Lollipop推出的导航抽屉。在阅读文档后,我发现我需要将抽屉覆盖在操作栏上。

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属性已设置。

toolbar.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>

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

正如您所看到的那样,它并没有扩大。此外,我没有添加statusBarColor,因为我的应用程序允许用户选择不同的颜色,因此它将是无用的。

非常感谢帮助!

1 个答案:

答案 0 :(得分: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>
相关问题