顶部 ActionBar 上的 Android 导航抽屉 [英] Android Navigation Drawer on top ActionBar

查看:30
本文介绍了顶部 ActionBar 上的 Android 导航抽屉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我主要活动的布局:

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout ...><相对布局 android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">...</相对布局>

stackoverflow 上的其他一些问题类似,例如 来实现你需要的效果.

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

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

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

<?xml version="1.0" encoding="utf-8"?><android.support.v4.widget.DrawerLayout ...><FrameLayout android:id="@+id/container"机器人:方向=垂直"android:layout_width="fill_parent"android:layout_height="fill_parent"/>

然后删除主布局中的 DrawerLayout.现在你的主要活动的布局应该看起来像

<RelativeLayout android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent">...</相对布局>

我们假设主活动的布局命名为main.xml".

在您的 MainActivity 中,编写如下:

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);设置内容视图(R.layout.main);//膨胀decor.xml"LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);DrawerLayout 抽屉 = (DrawerLayout) inflater.inflate(R.layout.decor, null);//空"很重要.//HACK:窃取"装饰视图的第一个孩子ViewGroup 装饰 = (ViewGroup) getWindow().getDecorView();查看孩子 = decor.getChildAt(0);装饰.removeView(子);FrameLayout 容器 = (FrameLayout) 抽屉.findViewById(R.id.container);//这是我们刚才定义的容器.容器.addView(child);//使抽屉替换第一个孩子装饰.addView(抽屉);//做你想做的事......}

现在你有了一个可以在 ActionBar 上滑动的 DrawerLayout.但是您可能会发现它被状态栏覆盖.您可能需要在 Drawer 中添加一个 paddingTop 来解决这个问题.

I'm trying to make the navigation drawer over the action bar when it was slide to the right like this app: [Removed]

This is my main activity's layout:

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

Some other questions on stackoverflow are similar such as this question but all answers are recommend to use sliding menu lib. But this app they still use android.support.v4.widget.DrawerLayout and they succeed. Don't ask me how I know they use the standard navigation drawer but I sure about it.

Would be really appreciate for your helps.


HERE IS THE FINAL SOLUTION: many thanks to @Peter Cai THIS WORKS PERFECTLY. https://github.com/lemycanh/DrawerOnTopActionBar

解决方案

I have a tiny "trick" learnt from https://github.com/jfeinstein10/SlidingMenu to implement the effect you required.

You only need to remove the first child of the window's decor view, and add the first child to your drawer's content view. After that, you only need to add your drawer to the window's decor view.

Below is some detailed steps for you to do that.

First, create a xml named "decor.xml" or anything you like. Only put the DrawerLayout and the drawer in. The "FrameLayout" below is just a container. We will use it to wrap your activity's content.

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

and then remove the DrawerLayout in your main layout. Now the layout of your main activity should look like

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

we assume that the main activity's layout is named "main.xml".

in your MainActivity, write as the following:

@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.......

}

Now you've got a DrawerLayout which can slide over the ActionBar. But you might find it covered by status bar. You might need to add a paddingTop to the Drawer in order to fix that.

这篇关于顶部 ActionBar 上的 Android 导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆