如何在 activity 开始时打开 drawerLayout?

How can I open the drawerLayout on activity start?

我想要一个 Activity 开头抽屉布局打开的星星。我试过简单地把

mDrawerLayout.openDrawer(gravity.END());

在我的代码中的不同位置,但不管项目变得不可点击,直到我关闭并重新打开抽屉。

有没有人对此有好运?添加此功能主要是出于用户体验的考虑。

我的抽屉布局中有一个列表视图,点击更改 activity 中的 RecyclerView。

这是我的 XML 文件,供可能需要的人使用。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:orientation="vertical">


<!--android:elevation="4dp"-->
<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->

<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 navigation drawer -->
    <ListView
        android:id="@+id/right_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:alpha="0.8"
        android:background="@color/colorPrimary"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            android:id="@+id/my_toolbar2"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:alpha="1"
            android:background="?attr/colorAccent"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat" />

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp"
        android:layout_below="@+id/my_toolbar2">



        <android.support.v7.widget.RecyclerView
            android:id="@+id/listview2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:background="@color/colorPrimary"
            android:dividerHeight="8dp" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal"
            android:src="@drawable/ic_add_circle_outline_white_24dp"
            android:visibility="visible"
            app:elevation="24dp"
            app:fabSize="auto"
            app:layout_anchor="@+id/listview2"
            app:layout_anchorGravity="bottom|end"
            app:rippleColor="#FFF" />


    </android.support.design.widget.CoordinatorLayout>
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

尝试在 ActivityonCreate(Bundle savedInstanceState) 中这样做:

    mDrawerLayout = findViewById(R.id.drawer_layout);
    mDrawerLayout.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (mDrawerLayout != null) {
                mDrawerLayout.openDrawer(Gravity.LEFT);
            }
        }
    }, 200);