Java – drawerlayout on the actionbar

When using drawer layout, is there any way to overlay the drawer view on the operation bar? I don't want to hide the action bar when displaying drawers I want the action bar to just stay placed, but send it to the background An example is the IOS play music app

My current implementation hides and displays the action bar when the drawer state changes, but I don't like this user experience

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

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

Solution

I searched the Internet to find any good way to solve this problem, but I didn't find it So I made such a skill

First, we need to request the action bar override function So in your activity's onCreate (), before setContntView (), you call it: requestWindowFeature (com.actionbarsherlock.view.Window.FEATURE_ACTION_BAR_OVERLAY);

It will draw everything including the navigation drawer behind the action bar. We don't need this, so we need to set the upper margin of FrameLayout. It uses the precise height of the action bar to host our clips in the activity In the active layout file, 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 only make the navigation drawer appear behind the operation bar Now, when the navigation drawer is half open, we will hide the action bar and show it when the drawer is almost closed To do this, we need to do 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 of the action bar to be paintable to make it transparent before I start hiding it, and change it back when I show it with my custom background

My custom background is a layerlistdrawable, which is transparent, but has a shaded separator at the bottom

To achieve this, I define the following list of layers 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>

In order 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,topOffset - 3,2);
        layerDrawable.setLayerInset(2,topOffset - 2,0);

        actionBar.setBackgroundDrawable(layerDrawable);

Where R. drawable shadow_ Divider is the list of XML layers I defined earlier

It looks really great! I hope it can help someone

edit

I have a bug here. Sometimes it may be the reason for infatuation This is a 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!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>