DrawerLayout在动作条的上方 [英] DrawerLayout on top of Actionbar

查看:129
本文介绍了DrawerLayout在动作条的上方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用抽屉布局是有没有办法覆盖在操作栏中抽屉看法?我不想隐藏操作栏上的抽屉时显示。我想操作栏简单地原地不动,但被发送到后台。这方面的一个例子是iOS的Play音乐应用...

我的当前实现隐藏和显示操作栏,当抽屉状态的变化,但我不喜欢这样的用户体验。

 公共无效onDrawerClosed(查看视图){
            getActionBar()显示()。
            invalidateOptionsMenu();
        }

        公共无效onDrawerOpened(查看drawerView){
            getActionBar()隐藏()。
            invalidateOptionsMenu();
        }
 

解决方案

我在网上搜索,以便找到这个问题什么好的解决办法,并没有发现。所以,我做了一个把戏,这样做。

首先,我们需要申请操作栏叠加功能。因此,在的onCreate()的活动,前 setContntView的()电话: requestWindowFeature(COM .actionbarsherlock.view.Window.FEATURE_ACTION_BAR_OVERLAY);

这会让一切,包括后面的动作栏导航抽屉平局。我们不需要这个,所以我们需要设置我们的它承载我们的片段与操作栏中的确切高度活性的FrameLayout的上边距。在活动的布局文件,我们需要做到以下几点:

 <! - 的FrameLayout显示片段 - >

    <的FrameLayout
        机器人:ID =@ + ID / frame_container
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:ATTR / actionBarSizelayout_marginTop = />
 

这将使只抽屉式导航栏出现操作栏后面。 现在,我们将隐藏在导航抽屉被打开了一半,并显示它时,抽屉几乎闭的动作吧。要做到这一点,我们需要在活动如下:

  @覆盖
    公共无效onDrawerSlide(查看drawerView,浮slideOffset){
        super.onDrawerSlide(drawerView,slideOffset);

        如果(slideOffset&0.5){
            actionBar.setBackgroundDrawable(空);
            actionBar.hide();
        } 其他 {
            actionBar.show();

            如果(slideOffset&所述; 0.1){
                actionBar.setBackgroundDrawable(layerDrawable);
            }
        }
    }
 

正如你所见,我也改变了操作栏的背景绘制,使之透明,当在我开始之前把它藏起来,并改变它带回我的自定义背景,当我表现出来了。

我的自定义背景是一个layerListDrawable这是全透明的,但在底部有一个分压器一些阴影。

和实现这一点,我已经定义了下面的图层列表中的XML:

 <层列表的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <项目的android:顶部=0dp机器人:左边=0dp安卓底=0dp机器人:右=0dp>
        <形机器人:形状=矩形>
            [固体机器人:颜色=@机器人:彩色/透明/>
        < /形状>
    < /项目>

    <项目的android:顶部=0dp机器人:左边=0dp安卓底=0dp机器人:右=0dp>
        <形机器人:形状=矩形>
            [固体机器人:颜色=#afafaf/>
        < /形状>
    < /项目>

    <项目的android:顶部=0dp机器人:左边=0dp安卓底=0dp机器人:右=0dp>
        <形机器人:形状=矩形>
            <梯度
                机器人:角=270
                机器人:startColor =#88afafaf
                机器人:endColor =#33afafaf
            />
        < /形状>
    < /项目>
< /层列表>
 

和获得我需要从这个XML我在活动如下背景:

 最后的动作条动作条= getSupportActionBar();
        最后LayerDrawable layerDrawable =(LayerDrawable)getResources()
                .getDrawable(R.drawable.shadow_divider);

        最后TypedArray styledAttributes = getTheme()。obtainStyledAttributes(
                新的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);
 

在这里R.drawable.shadow_divider就是我刚才定义的XML层列表。

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

修改

我有一个小虫子在这里它可以是一个暗恋的理由的时候。这里是固定的code:   `

 <的FrameLayout
        机器人:ID =@ + ID / frame_container
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        ?ATTR / actionBarSize:**机器人paddingTop = ** />`
 

这应该是 paddingTop 不是 layout_marginTop

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

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();
        }

解决方案

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);

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

And to achieve this I have defined the following layer-list in 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:

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.

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 paddingTop not layout_marginTop!

这篇关于DrawerLayout在动作条的上方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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