其他分享
首页 > 其他分享> > 顶部ActionBar上的Android导航抽屉

顶部ActionBar上的Android导航抽屉

作者:互联网

我正试图将导航抽屉放在操作栏上,当它向右滑动时,就像这个应用程序:
[删除]

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

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

解决方法:

我从https://github.com/jfeinstein10/SlidingMenu学到一个小技巧,以实现你所需要的效果.

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

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

首先,创建一个名为“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.但您可能会发现状态栏覆盖了它.您可能需要向Drawer添加paddingTop才能解决问题.

标签:android,navigation,android-actionbar,drawer
来源: https://codeday.me/bug/20190918/1811581.html