展开导航抽屉的动作栏/工具栏
问题描述:
我一直在实现我的一个应用程序,并添加了与棒棒糖一起出现的导航抽屉。在阅读文档时,我发现我需要让抽屉覆盖操作栏。展开导航抽屉的动作栏/工具栏
搜索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>
通知的fitsSystemWindows属性设置。
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" />
值-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,因为我的应用程序允许用户选择不同的颜色,所以它是没用的。
帮助表示赞赏!
答
我相信你需要修改你的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>
这是主要布局? – 2015-03-03 08:19:37
@AndrewQ是的,activity_nav_drawer.xml – 2015-03-03 08:21:41
嗯...好吧...让我做一些测试! – 2015-03-03 08:25:36