打开导航抽屉onCreate

时间:2014-12-22 14:24:26

标签: android navigation-drawer oncreate

我目前正在尝试将导航抽屉实施到我的应用程序中。为此,我希望在活动开始时打开抽屉。这是我的onCreate:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_master);
    isInLandscapeMode = getResources().getBoolean(R.bool.isInLandscape);

    if (!isInLandscapeMode)
    {
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer, R.string.date, R.string.applicationName)
        {
            /**
             * Called when a drawer has settled in a completely closed
             * state.
             */
            public void onDrawerClosed(View view)
            {
                super.onDrawerClosed(view);
            }

            /** Called when a drawer has settled in a completely open state. */
            public void onDrawerOpened(View drawerView)
            {
                super.onDrawerOpened(drawerView);
                drawerView.bringToFront();
                drawerView.requestLayout();
            }
        };

        // Set the drawer toggle as the DrawerListener
        drawerLayout.setDrawerListener(drawerToggle);
        drawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

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

    drawerLayout.openDrawer(Gravity.START);
}

抽屉正在打开,但如果我尝试点击其中一个列表条目,它就会关闭。如果我之后手动打开它,一切都很完美。我是否必须以某种方式将焦点放在我的抽屉上?我尝试了几件事,但似乎没什么用。

更新

终于找到了我的错误:

我在布局xml文件中有以下代码:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

        <LinearLayout
            android:id="@+id/list_container"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#f1f1f1"
            android:orientation="horizontal" >
        </LinearLayout>

        <FrameLayout
            android:id="@+id/detail_container"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
        </FrameLayout>
    </android.support.v4.widget.DrawerLayout>

</FrameLayout>

主要内容视图(detail_container)必须是导航抽屉思想中的第一个子视图。所以我改为:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

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

        <FrameLayout
            android:id="@+id/detail_container"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
        </FrameLayout>

        <LinearLayout
            android:id="@+id/list_container"
            android:layout_width="320dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#f1f1f1"
            android:orientation="horizontal" >
        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>

</FrameLayout>

它有效。

2 个答案:

答案 0 :(得分:0)

使用drawer.openDrawer(Gravity.LEFT);

答案 1 :(得分:0)

找到您的抽屉布局和数据(列表或可扩展列表)后,然后输入

  drawerLayout.openDrawer("Name of Data Contained By Drawer");