【发布时间】: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