如何在NavigationView中实现CoordinatorLayout和工具栏

时间:2018-09-07 18:51:58

标签: android android-toolbar navigationview coordinator-layout

是否可以实现:使用CoordinatorLayout在NavigationView中使用工具栏

代码正确吗?

请给我一个例子,并帮助我做到这一点 看到照片以获得更好的理解

picture

activity_main.xml

<!-- notation start -->
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout >
    <android.support.design.widget.CoordinatorLayout>
        <android.support.design.widget.AppBarLayout>
            <android.support.design.widget.CollapsingToolbarLayout>
                <ImageView/>
                <android.support.v7.widget.Toolbar>
                </android.support.v7.widget.Toolbar>
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
        <android.support.v4.widget.NestedScrollView
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!-- content  -->
        </android.support.v4.widget.NestedScrollView>
        <android.support.design.widget.FloatingActionButton/>
    </android.support.design.widget.CoordinatorLayout>

<!-- notation end-->


      <!-- NavigationView  -->

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_activity_main"
        app:menu="@menu/activity_main_drawer" />

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

nav_header_activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout>
        <android.support.design.widget.AppBarLayout>
            <android.support.design.widget.CollapsingToolbarLayout>
                <ImageView/><!-- background Toolbar-->
                <android.support.v7.widget.Toolbar>
                <!-- app:layout_collapseMode="pin" -->
                </android.support.v7.widget.Toolbar>
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
        <android.support.v4.widget.NestedScrollView
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!-- START content NavigationView header -->
            <LinearLayout>
                <ImageView/>
                <TextView/>
                <TextView/>
            </LinearLayout>
<!--END content NavigationView header -->
        </android.support.v4.widget.NestedScrollView>
        <android.support.design.widget.FloatingActionButton/>
    </android.support.design.widget.CoordinatorLayout>

菜单/activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item/>
        <item/>
    </group>
    <item android:title="Communicate">
        <menu>
            <item/>
            <item/>
        </menu>
    </item>
<!-- 20 item in menu -->
</menu>

任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:0)

您在上述xml代码中缺少一些属性

1-您没有为appbarlayout,CollapsingToolbarLayout和NestedScrollView设置layout_height和layout_width。

您的activity_main应该如下所示:

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

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

 <!--specify your header layout in NavigationView-->
<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_activity_main"
    app:menu="@menu/activity_main_drawer" />

2-您的nav_header_activity_main.xml布局应在NavigationView中进行设置,例如(app:headerLayout =“ @ layout / nav_header_activity_main.xml”),并且您应该只放置标题内容的小部件,并摆脱那些协调布局,appbar, nav_header中的collapsingtoolbar和工具栏

app_bar_main.xml(导航栏布局)

<?xml version="1.0" encoding="utf-8"?> 
   <android.support.design.widget.CoordinatorLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
       android:layout_width="match_parent"
       android:layout_height="192dp"
       android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>


<!-- define layout for main content here with NestedScrolView-->
<include layout="@layout/content_main" />



</android.support.design.widget.CoordinatorLayout>