UPDATE:如何用叠加导航抽屉的动作条。 (使用新的工具栏) 在你的build.gradle
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:support-v4:21.0.0'
这使用这些在你的依赖作为你drawerlayout
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layout_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar"/>
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"/>
</LinearLayout>
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/list_background"
/>
</android.support.v4.widget.DrawerLayout>
结交新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"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />
转到您的活动扩展导航抽屉。 和之后的setContentView添加此()
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
不要忘记延长你的主题NoActionBar在你的价值观的文件夹。
<style name="Theme.Whtsnxt" parent="@style/Theme.AppCompat.Light.NoActionBar">
<item name="windowActionBar">false</item>
<!-- colorPrimary is used for the default action bar background -->
<item name="windowActionModeOverlay">true</item>
<item name="android:textColorPrimary">@color/white</item>
<item name="colorPrimary">@color/splashscreen</item>
<item name="colorPrimaryDark">@color/holo_blue_light</item>
<item name="android:windowBackground">@color/white</item>
<item name="android:colorBackground">@color/white</item>
</style>
确认这个破解工作正常。 我们需要添加填充到抽屉,这里是获取状态栏高度的技巧:http://stackoverflow.com/questions/ 20584325 /可靠获取高度状态条解决kitkat半透明导航问题。 另一个破解是我们想要添加片段到容器,而不是像这样添加:fragmentTransaction.replace(R .id.contai片段); 我们应该替换为R.id.content。 fragmentTransaction.replace(getContentIdResource(),fragment); ... private int getContentIdResource(){ \t \t return getResources()。getIdentifier(“content”,“id”,“android”); \t} – lemycanh
@lemycanh我用来添加片段的另一个解决方案是在主布局中添加一个容器。抽屉布局已经从主布局中分离出来,所以R.id.container只打包在main.xml中定义的布局。 –
伟大的答案!在实现这个之后,我意识到我们实际上是从另一个布局中窃取了一个视图并放弃它!哈哈 – uLYsseus