Java Actionbar 顶部的 DrawerLayout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21884675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:15:40  来源:igfitidea点击:

DrawerLayout on top of Actionbar

javaandroidandroid-actionbarnavigation-drawerdrawerlayout

提问by MCR

When using the drawer layout is there a way to overlay the drawer view over the action bar? I do not want to hide the action bar when the drawer is shown. I want the action bar to simply stay put, but be sent to the background. An example of this is the iOS Play Music app...

使用抽屉布局时,有没有办法将抽屉视图覆盖在操作栏上?我不想在显示抽屉时隐藏操作栏。我希望操作栏保持原状,但被发送到后台。这方面的一个例子是 iOS Play 音乐应用程序......

enter image description here

在此处输入图片说明

My current implementation hides and shows the action bar when the drawer state changes, but I do not like this user experience.

我当前的实现在抽屉状态更改时隐藏和显示操作栏,但我不喜欢这种用户体验。

        public void onDrawerClosed(View view) {
            getActionBar().show();
            invalidateOptionsMenu(); 
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().hide();
            invalidateOptionsMenu();
        }

回答by thelawnmowerman

That's the proper effect in Android defaults. If you want to emulate the iOS effect, you'll probably have to do it yourself, because AFAIK the drawer component of the support library doesn't allow such type of configurations.

这是 Android 默认设置中的正确效果。如果你想模拟 iOS 效果,你可能必须自己做,因为 AFAIK 支持库的抽屉组件不允许这种类型的配置。

Anyway, every time a programmer codes a lot just to emulate some fancy (and much probably not worthy) iOS effect in Android, a little cat dies...

无论如何,每当程序员编写大量代码只是为了在 Android 中模拟一些奇特的(而且很可能不值得)iOS 效果时,一只小猫就会死...

回答by Submersed

I don't think you'll be able to do this with Android's provided Navigation Drawer (without doing something really hacky), since doing this really goes against Android design patterns. If you want a library that does what you're looking for, I've used thislibrary before Android came out with it's own widget, and it does what you're looking for.

我认为您无法使用 Android 提供的 Navigation Drawer 来做到这一点(无需做一些非常棘手的事情),因为这样做确实违反了 Android 设计模式。如果您想要一个可以满足您需求的库,我在 Android 推出自己的小部件之前就使用过这个库,它可以满足您的需求。

回答by mass441

You can achieve this with ActionBarSherlock.

您可以使用 ActionBarSherlock 实现这一点。

http://actionbarsherlock.com/

http://actionbarsherlock.com/

回答by Atul O Holic

You will be able to do it but the drawer will look so awkward that you will not like it. I had the same requirement and I ended up using Radio Buttons customized to appear as tabs. You can also try hiding the tabs when the drawer opens and vice versa, but that certainly is not what you want here.

你可以这样做,但抽屉看起来很笨拙,你不会喜欢它。我有同样的要求,我最终使用了定制的单选按钮以显示为选项卡。您也可以尝试在抽屉打开时隐藏选项卡,反之亦然,但这肯定不是您想要的。

You can try to use ViewPager, tabbed indicator such PagerTabStrip and/or TabPageIndicator.

您可以尝试使用 ViewPager、选项卡式指示器,例如 PagerTabStrip 和/或 TabPageIndicator。

Source.

来源

回答by Andranik

I searched the web in order to find any good solution for this problem and haven't found. So I did a trick that did this.

我在网上搜索以找到解决此问题的任何好的方法,但没有找到。所以我做了一个技巧来做到这一点。

First of all we need to request action bar overlay feature. So in onCreate()of your activity, before setContntView()call: requestWindowFeature(com.actionbarsherlock.view.Window.FEATURE_ACTION_BAR_OVERLAY);

首先,我们需要请求操作栏覆盖功能。所以在onCreate()你的活动中,在setContntView()打电话之前:requestWindowFeature(com.actionbarsherlock.view.Window.FEATURE_ACTION_BAR_OVERLAY);

It will make everything including navigation drawer draw behind action bar. We don't need this, so we need to set the top margin of our FrameLayout which hosts our fragments in the activity with the exact height of the action bar. In the layout file of the activity we need to do the following:

它将使包括导航抽屉在内的所有内容都绘制在操作栏后面。我们不需要这个,所以我们需要设置我们的 FrameLayout 的上边距,它在活动中托管我们的片段,并具有操作栏的确切高度。在活动的布局文件中,我们需要做以下事情:

<!-- Framelayout to display Fragments -->

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize" />

It will make only navigation drawer appear behind the action bar. Now we will hide the action bar when the navigation drawer is opened on half, and will show it when the drawer is nearly closed. To do this we need to make the following in the activity:

它只会让导航抽屉出现在操作栏后面。现在我们将在导航抽屉打开一半时隐藏操作栏,并在抽屉几乎关闭时显示它。为此,我们需要在活动中进行以下操作:

@Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        super.onDrawerSlide(drawerView, slideOffset);

        if(slideOffset > 0.5){
            actionBar.setBackgroundDrawable(null);
            actionBar.hide();
        } else {
            actionBar.show();

            if(slideOffset < 0.1){
                actionBar.setBackgroundDrawable(layerDrawable);
            }
        }       
    }

As you can see I also change the background drawable of the action bar to make it transparent when before I begin to hide it, and change it back with my custom background when I show it back.

如您所见,我还更改了操作栏的可绘制背景,使其在开始隐藏之前透明,并在显示时使用自定义背景将其更改回来。

My custom background is a layerListDrawable which is all transparent but have a divider in bottom with some shadow.

我的自定义背景是一个 layerListDrawable,它是透明的,但底部有一个带有阴影的分隔线。

And to achieve this I have defined the following layer-list in XML:

为了实现这一点,我在 XML 中定义了以下层列表:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/transparent"/>
        </shape>
    </item>

    <item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
        <shape android:shape="rectangle">
            <solid android:color="#afafaf"/>
        </shape>
    </item>

    <item android:top="0dp" android:left="0dp" android:bottom="0dp" android:right="0dp">
        <shape android:shape="rectangle">
            <gradient
                android:angle="270"
                android:startColor="#88afafaf" 
                android:endColor="#33afafaf"
            />
        </shape>
    </item>    
</layer-list>

And to get the background I need from this XML I do the following in the activity:

为了从这个 XML 获得我需要的背景,我在活动中执行以下操作:

final ActionBar actionBar = getSupportActionBar();
        final LayerDrawable layerDrawable = (LayerDrawable) getResources()
                .getDrawable(R.drawable.shadow_divider);

        final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
                new int[] { R.attr.actionBarSize });
        int topOffset = (int) (styledAttributes.getDimension(0, 0));
        styledAttributes.recycle();

        layerDrawable.setLayerInset(1, 0, topOffset - 3, 0, 2);
        layerDrawable.setLayerInset(2, 0, topOffset - 2, 0, 0);

        actionBar.setBackgroundDrawable(layerDrawable);

where R.drawable.shadow_divider is the XML layer-list I have defined earlier.

其中 R.drawable.shadow_divider 是我之前定义的 XML 图层列表。

It looks really great! Hope it can help someone.

看起来真的很棒!希望它可以帮助某人。

EDIT

编辑

I had a small bug here which can be a reason of a crush sometimes. Here is the fixed code: `

我在这里有一个小错误,有时可能是我迷恋的原因。这是固定代码:`

    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        **android:paddingTop="?attr/actionBarSize"**  />`

It should be paddingTopnot layout_marginTop!

它应该是paddingTop而不是layout_marginTop

回答by lemycanh

I found the answer in my question, also added an example project for reference. You can take a look to see if it works for you. Android Navigation Drawer on top ActionBar

我在我的问题中找到了答案,还添加了一个示例项目以供参考。你可以看看它是否适合你。 顶部 ActionBar 上的 Android 导航抽屉