トラッキング コード

12/21/2013

How to use the DrawerLayout like a slide menu of the "Google Play"

Android Developers Site

http://developer.android.com/training/implementing-navigation/nav-drawer.html


Creat a layout file

You shoud create a layout file using DrawerLayout in android.support.v4.widget.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- The main content view -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- The navigation drawer -->

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>

Set in Activity

We set ActionBarDrawerToggle.
We call "setHomeButtonEnabled(true)" and "setDisplayHomeAsUpEnabled(true)", in order to enable the click of an icon on ActionBar.

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawerlayout);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        mTitle = getTitle();
        mDrawerTitle = getText(R.string.drawer_open);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerToggle = new ActionBarDrawerToggle(
                this, /* host Activity */
                mDrawerLayout, /* DrawerLayout object */
                R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
                R.string.drawer_open, /* "open drawer" description */
                R.string.drawer_close /* "close drawer" description */
                ) {

                    /**
                     * Called when a drawer has settled in a completely closed
                     * state.
                     */
                    public void onDrawerClosed(View view) {
                        getActionBar().setTitle(mTitle);
                    }

                    /**
                     * Called when a drawer has settled in a completely open
                     * state.
                     */
                    public void onDrawerOpened(View drawerView) {
                        getActionBar().setTitle(mDrawerTitle);
                    }
                };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);
    }

No comments:

Post a Comment