【问题标题】:Fragment Navigation Drawer Initializes as Open片段导航抽屉初始化为打开
【发布时间】:2015-11-11 04:19:02
【问题描述】:

我正在尝试使用自定义布局创建一个导航抽屉(我知道它已经过时了)。我正在使用 NavDrawer 中的片段来完成此操作。 我遇到的问题是,当我运行我的应用程序时,我的导航抽屉会自动打开,我无法关闭它/不知道如何关闭它。我怀疑这与我的片段事务结束时的 .commit() 方法有关。

所以我的问题是如何让包含片段的导航抽屉像只包含可以拖入和拖出的 ListView 的普通导航抽屉一样操作?或者,如果一个片段在导航抽屉中造成太多困难,是否有另一种方法可以在导航抽屉内实现超级自定义布局?提前致谢!

这就是它的样子。代码如下图...

在 MapsActivity.java 中:

//Instantiates Fragment inside of our Nav Drawer        
    NavigationDrawerFragment navDrawerFragment = new                    NavigationDrawerFragment();
    getFragmentManager().beginTransaction()
            .add(R.id.navDrawer_container,
navDrawerFragment).addToBackStack(null).commit();

activity_maps.xml:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF">

    <fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize" />

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorMapToolbar"
        android:minHeight="?attr/actionBarSize"
        app:contentInsetEnd="0dp"
        app:contentInsetStart="0dp">
        <!-- contentInsert is the initial padding that is added by the system at the start -->


        <RelativeLayout
            android:id="@+id/topLayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize">

            <ImageView
                android:id="@+id/back_action"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_marginLeft="6dp"
                android:layout_marginStart="6dp"
                android:gravity="center_vertical"
                android:padding="6dp"
                android:src="@drawable/icon_goleft" />


            <ImageView
                android:id="@+id/locker"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_marginEnd="6dp"
                android:layout_marginRight="6dp"
                android:gravity="center_vertical"
                android:padding="6dp"
                android:src="@drawable/lock_open" />

        </RelativeLayout>

    </android.support.v7.widget.Toolbar>

    <View
        android:id="@+id/dropshadow"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:layout_marginTop="?attr/actionBarSize"
        android:background="@drawable/dropshadow_dark" />

    <ImageButton
        android:id="@+id/floatingActionImageButton"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_margin="15dp"
        android:background="@drawable/circle"
        android:focusable="true"
        android:src="@drawable/icon_location" />

    <LinearLayout
        android:id="@+id/markerLayout"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:background="@color/colorMapToolbar"
        android:orientation="horizontal"
        android:weightSum="2">

        <TextView
            android:id="@+id/markerCancel"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/cancel"
            android:textColor="@android:color/white"
            android:textSize="@dimen/abc_text_size_large_material" />

        <TextView
            android:id="@+id/markerMark"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/mark"
            android:textColor="@color/colorAccent"
            android:textSize="@dimen/abc_text_size_large_material"
            android:textStyle="bold" />

    </LinearLayout>
</RelativeLayout>
<!-- FRAGMENT CONTAINER -->
<FrameLayout
    android:id="@+id/navDrawer_container"
    android:layout_width="240dp"
    android:layout_height="match_parent" />

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

fragment_navigation_drawer.xml:(附带一个.java for Fragment,但这里不相关)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="200dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2"
android:background="@android:color/white">

<RelativeLayout
    android:id="@+id/topRowLayout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:padding="8dp">

    <TextView
        android:id="@+id/userTitleBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:padding="8dp"
        android:text="@string/userTitle"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/alertTitleBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:padding="8dp"
        android:text="@string/alertTitle"
        android:textStyle="bold" />

    <TextView android:id="@+id/lastFlamText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:gravity="center_horizontal"
        android:text="@string/lastFlameTitle"
        android:layout_below="@+id/userTitleBtn" />

    <TextView
        android:id="@+id/scoreTextNavDrawer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_horizontal|bottom"
        android:padding="16dp"
        android:textSize="100sp"
        android:textStyle="bold"
        android:layout_below="@+id/lastFlamText" />

</RelativeLayout>


<ListView
    android:id="@+id/navList"
    android:layout_width="240dp"
    android:layout_height="0dp"
    android:layout_gravity="left|start"
    android:layout_weight="1" />
</LinearLayout>

【问题讨论】:

    标签: android android-layout android-fragments android-activity navigation-drawer


    【解决方案1】:

    android:layout_gravity="left|start" 属性需要在activity_maps 布局中继续navDrawer_container FrameLayout。这个属性让DrawerLayout 知道这个View 是抽屉。它丢失的事实可以解释为什么您的抽屉在启动时“打开”并且无法移动。

    <FrameLayout
        android:id="@+id/navDrawer_container"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start" />
    

    您现在不一定需要在 ListView 布局中的 fragment_navigation_drawer 上使用该属性。

    【讨论】:

      【解决方案2】:

      将此代码添加到您的活动或片段中

      @Override
            public void onStart() {
                   super.onStart();
                   if (drawer_layout.isDrawerOpen(Gravity.LEFT)) {
                       drawer_layout.closeDrawer(Gravity.LEFT);
                   }
               }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-13
        • 1970-01-01
        • 1970-01-01
        • 2018-01-16
        • 2016-01-03
        相关资源
        最近更新 更多