顶部 ActionBar 上的 Android 抽屉导航

标签 android navigation android-actionbar drawer

当它像这个应用程序一样向右滑动时,我正在尝试在操作栏上制作抽屉导航: [已删除]

这是我主要 Activity 的布局:

<?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 上的其他一些问题类似,例如 this question但所有答案都建议使用滑动菜单库。但是这个应用程序他们仍然使用 android.support.v4.widget.DrawerLayout 并且他们成功了。不要问我怎么知道他们使用标准的抽屉式导航,但我很确定。

非常感谢您的帮助。


这是最终解决方案:非常感谢 @Peter Cai 这非常有效。 https://github.com/lemycanh/DrawerOnTopActionBar

Screen Capture Translucent on KitKat

最佳答案

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

您只需要删除窗口装饰 View 的第一个 child ,并将第一个 child 添加到抽屉的内容 View 中。之后,您只需要将抽屉添加到窗口的装饰 View 中即可。

以下是您执行此操作的一些详细步骤。

首先,创建一个名为“decor.xml”的 xml 或任何你喜欢的东西。只把DrawerLayout和抽屉放进去。下面的“FrameLayout”只是一个容器。我们将使用它来包装您的 Activity 内容。

<?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。现在你的主要 Activity 的布局应该看起来像

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

我们假设主 Activity 的布局命名为“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。但是您可能会发现它被状态栏覆盖。您可能需要在 Drawer 中添加一个 paddingTop 来解决这个问题。

关于顶部 ActionBar 上的 Android 抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23294954/

相关文章:

android - 如何使SearchView覆盖整个ActionBar/Toolbar

android - 带有按钮的 ListView,但只有按钮被选中,而不是列表

Android:在 EditText 焦点上的自定义 AlertDialog 中显示软键盘

css - super 滑出式菜单

jquery - 使用 href ="#!/id"导航

android - 如何去除 ActionBar 上应用程序图标和屏幕边缘之间的边距?

java - fragment 中的 RecyclerView 从 Activity 返回后为空

Android 连接 USB 时触摸屏有延迟

jquery - Css子菜单全宽,两列不起作用

android - 操作栏显示在其选项卡下方