安卓导航抽屉上面动作条抽屉、动作

2023-09-12 01:33:52 作者:Exo 致命的王者

我试图使导航抽屉在操作栏时,它是幻灯片喜欢这个程序的权利: [删除]

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>

计算器上的其他一些问题是相似的,如这个问题但所有的答案都推荐使用滑动菜单库。但是,这个应用程序,他们仍然使用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.

将是非常美联社preciate您的帮助。

Would be really appreciate for your helps.

这是最终的解决方案:非常感谢 @Peter蔡这工作完全。 https://github.com/lemycanh/DrawerOnTopActionBar

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

推荐答案

我有一个小绝招从 HTTPS了解到:// github上。 COM / jfeinstein10 / SlidingMenu 来实现你需要的效果。

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.

首先,创建一个名为decor.xml或者任何你喜欢的XML。只有把DrawerLayout和抽屉里的的FrameLayout的仅仅是一个容器。我们将用它来包装你的行为的内容。

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>

然后删除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>

我们假设的主要活动的布局被命名为main.xml中。

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

在你的MainActivity,编写如下:

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

}

现在你已经有了可以滑过动作条一DrawerLayout。但是,你可能会发现它涵盖的状态栏。您可能需要一个paddingTop添加到抽屉才能解决这个问题。

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.