Android Studio DrawerLayer

Android Studio DrawerLayer

所以我尝试将抽屉布局添加到我的项目中。一开始没用,然后我打开一个新项目测试也没用。

<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:choiceMode="singleChoice"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:background="#111"/>

没有显示任何内容,既不是图标也不是抽屉本身。 谢谢你的帮助。

仅布局不足以实现有效的 DrawerLayout。您必须额外准备 activity.

请分析下面的代码:

 Activity

    //You have to use ActionBarActivity from support library
    public class ActivityWithSliderDrawer extends ActionBarActivity{
        //you need this object to synchronise state with your activity
        ActionBarDrawerToggle mActionBarDrawerToggle;
        DrawerLayout mDrawerLayout;
        @Override
        public void onCreate(Bundle savedInstanceState ) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            //here you create ActionBarDrawerToggle object
            mActionBarDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                    R.string.menu_is_open, R.string.menu_is_close);
            //you set it for Drawer layout
            mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);

            //if you want to display indicator 
            // you have to call two methods below
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
            // If you ever will use toolbar, you have to call additionally
            //setSupportActionBar(toolbarObject)
            //but before these first two methods
        }

        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            //you have to call syncState() method on ActionBarDrawerToggle object 
            // here onPostCreate(..) method
            super.onPostCreate(savedInstanceState);
            mActionBarDrawerToggle.syncState();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            //you have to call onConfigurationChanged(...) method on ActionBarDrawerToggle object 
            // here onConfigurationChanged(..) method
            mActionBarDrawerToggle.onConfigurationChanged(newConfig);
        }
    }

activity_test.xml - 布局

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        xmlns:android="http://schemas.android.com/apk/res/android"
        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="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="@android:color/white"/>
    </android.support.v4.widget.DrawerLayout>

清单中的声明

    <activity
        android:name="your.package.ActivityWithSliderDrawer"
        //here is important part of implentation- you have to use Theme.AppCompat
        //or use toolbar
        android:theme="@style/Theme.AppCompat"
        android:windowSoftInputMode="adjustResize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>