顶部的ActionBar上的Android导航抽屉

我正在尝试使滑动到右侧像这个应用程序的操作栏上的导航抽屉:[Removed]

这是我的主要活动的布局:

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout ...> <RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> ... </RelativeLayout> <fragment android:name="com...." android:layout_gravity="start" android:id="@id/navigation" android:layout_width="@dimen/navigation_menu_width" android:layout_height="fill_parent" /> </android.support.v4.widget.DrawerLayout> 

一些其他问题在stackoverflow是类似的这个问题,但所有的答案都build议使用滑动菜单库。 但这个应用程序,他们仍然使用android.support.v4.widget.DrawerLayout,他们成功。 不要问我怎么知道他们使用标准的导航抽屉,但我确定。

将非常感谢您的帮助。


这是最终的解决scheme :非常感谢@Peter Cai这是完美的工作。 https://github.com/lemycanh/DrawerOnTopActionBar

屏幕捕捉半透明的KitKat

我从https://github.com/jfeinstein10/SlidingMenu获得了一个小小的“技巧”来实现你所需要的效果。;

您只需要移除窗口装饰视图的第一个子项,并将第一个子项添加到抽屉的内容视图中。 之后,您只需将抽屉添加到窗户的装饰视图中即可。

下面是一些你要做的详细步骤。

首先,创build一个名为“decor.xml”的xml或者任何你喜欢的东西。 只能放置DrawerLayout和抽屉。下面的“FrameLayout”只是一个容器。 我们将用它来包装你的活动的内容。

 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout ...> <FrameLayout android:id="@+id/container" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <fragment android:name="com...." android:layout_gravity="start" android:id="@id/navigation" android:layout_width="@dimen/navigation_menu_width" android:layout_height="fill_parent" /> </android.support.v4.widget.DrawerLayout> 

然后删除主布局中的DrawerLayout。 现在你的主要活动的布局应该是这样的

 <RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> ... </RelativeLayout> 

我们假定主要活动的布局被命名为“main.xml”。

在你的MainActivity中,写如下:

 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Inflate the "decor.xml" LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); DrawerLayout drawer = (DrawerLayout) inflater.inflate(R.layout.decor, null); // "null" is important. // HACK: "steal" the first child of decor view ViewGroup decor = (ViewGroup) getWindow().getDecorView(); View child = decor.getChildAt(0); decor.removeView(child); FrameLayout container = (FrameLayout) drawer.findViewById(R.id.container); // This is the container we defined just now. container.addView(child); // Make the drawer replace the first child decor.addView(drawer); // Do what you want to do....... } 

现在你已经有了一个可以在ActionBar上滑动的DrawerLayout。 但是你可能会发现它由状态栏覆盖。 您可能需要添加一个paddingTop到抽屉来解决这个问题。

更新:如何覆盖与导航抽屉的操作栏。 (使用新的工具栏)在build.gradle的依赖关系中使用这些工具栏

 compile 'com.android.support:appcompat-v7:21.0.0' compile 'com.android.support:support-v4:21.0.0' 

这是你的抽屉

 <!-- 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> 

在您的布局文件夹中创build新的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> 

我已经发布了一个技巧,使之前的Android L版本可能。 你可以在这篇文章中find我的解决scheme。 希望对某人有用。