无法在工具栏下方导航抽屉

时间:2017-10-27 13:26:52

标签: android android-layout

如何将导航抽屉安装在工具栏下方,它与主要布局重叠 activity_set_main.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true"
    tools:openDrawer="start"
    >

<include
    layout="@layout/app_bar_set_main"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#000000"
    android:layout_marginTop="?android:attr/actionBarSize"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:itemTextColor ="#FFFFFF"
    app:headerLayout="@layout/nav_header_set_main"
    app:menu="@menu/activity_set_main" />

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

app_bar_set_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"
    android:fitsSystemWindows="true"
    tools:context="com.example.pash.icas_nvod.setMain">

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"

       >
        <ImageView
            android:layout_width="220dp"
            android:layout_height="40dp"
            android:src="@drawable/bydesign_logo"
            android:layout_marginRight="80dp"
            android:id="@+id/toolbar_title" />
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_set_main" />
</android.support.design.widget.CoordinatorLayout>

content_set_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/content_set_main_drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#EE7F22"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.example.pash.icas_nvod.setMain"
    >
    <ProgressBar
        android:id="@+id/login_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone" />
</RelativeLayout>

以上是我的xml文件无法使抽屉适合工具栏。 抽屉布局与主布局重叠。如何解决它..? 我搜索了很多,但没有一个答案与我的问题有关。提前谢谢你。

1 个答案:

答案 0 :(得分:2)

您可以在android:layout_below="@+id/app_bar_set_main"中尝试DrawerLayout

- - - - - - - - OR

您可以尝试将工具栏移出抽屉布局。所以在我的项目中,我使用LinearLayout作为根布局。我试图用你的组件替换,希望它有所帮助。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<include
layout="@layout/app_bar_set_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"

/>

<android.support.v4.widget.DrawerLayout 
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true"
tools:openDrawer="start"
>

<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_below="@+id/app_bar_set_main" //Add this line also.
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#000000"
android:layout_marginTop="?android:attr/actionBarSize"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemTextColor ="#FFFFFF"
app:headerLayout="@layout/nav_header_set_main"
app:menu="@menu/activity_set_main" />

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


</LinearLayout>

修改:您还必须添加android:layout_below以适合工具栏下的抽屉。