【问题标题】:How can I open the drawerLayout on activity start?如何在活动开始时打开抽屉布局?
【发布时间】:2017-09-22 22:10:04
【问题描述】:

我想要一个在开始时打开抽屉布局的 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>

【问题讨论】:

    标签: java android xml android-layout drawerlayout


    【解决方案1】:

    尝试在您的onCreate(Bundle savedInstanceState)Activity 中这样做:

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

    【讨论】:

    • 这也很棒,因为现在用户可以看到抽屉打开的动画,而不仅仅是在启动时打开。
    猜你喜欢
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多