android - 如何将抽屉导航放在工具栏下方?

标签 android navigation-drawer toolbar

我的抽屉导航在工具栏上方。我还添加了一些 xml 代码。请帮助我。

Here my navigation drawer is above toolbar

这是我的activity.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:fitsSystemWindows="true"
    tools:openDrawer="start">

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

    <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:menu="@menu/activity_main2_drawer" />

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

和我的 app_bar 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.ezybzy.ezybzy.categoris">

    <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="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_categoris" />

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

和我的内容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: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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.ezybzy.ezybzy.categoris"
    tools:showIn="@layout/app_bar_categories"
    android:background="#ffffff">
</RelativeLayout>

我使用 android studios 抽屉导航 Activity 创建了抽屉导航..

最佳答案

当然 android:layout_marginTop="?attr/actionBarSize"

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?attr/actionBarSize">

但问题是 drawerlayout 位于工具栏的顶部。这就是这里衰落的原因。 您可以通过以下方式消除褪色

mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));

但在某些设备上它可能看起来有线。

解决方案

使用 Android Studio 时。我们可以创建 NavigationDrawerActiviity 有3个文件名为

activity_main.xml

app_bar_main.xml

nav_header_main.xml

content_main.xml

因此我们可以跳过 app_bar_main.xml 并且我们可以移除褪色。

第一步

将 Activity 的 Root View 设为 Vertical LinearLayout

<LinearLayout 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:orientation="vertical"
android:fitsSystemWindows="true"
tools:context="com.example.MainActivity">
</LinearLayout>

activity_main.xml中添加DrawerLayout并在DrawerLayout中包含content_main.xml。并在 DrawerLayout 上方添加 AppBarLayout

<LinearLayout 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:orientation="vertical"
tools:context="com.qproinnovations.schoolmanagement.activity.HomeActivity">
    <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="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" >

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

    </android.support.design.widget.AppBarLayout>
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">
    <!-- drawer view -->
        <include layout="@layout/content_main" />
<!-- drawer content -->
        <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:menu="@menu/activity_home_drawer" />
    </android.support.v4.widget.DrawerLayout>

</LinearLayout>

第二步

将NavigationDrawerActiviity的setContentView()添加替换为

setContentView(R.layout.activity_main);

终于有了

enter image description here

关于android - 如何将抽屉导航放在工具栏下方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35810384/

相关文章:

android - Whatsapp 喜欢折叠工具栏

android - 在 Android Studio 中定义小部件工具栏时出错

android - 将Firebase添加到flutter项目中,破坏了项目

android - 如何按 Intent 传递对象

android - 使用带有喷气背包导航组件的抽屉导航时如何更改工具栏图标(汉堡图标)

android - 带抽屉导航的 PreferenceActivity 抛出空指针异常

javascript - 使用createDrawerNavigator时如何让Header也滑动?

android - 在 Library 22 中用什么代替 getSupportActionBar()?

android - 如何更改 CollapsingToolbarLayout 字体和大小?

android - 在 Android 中使用缩放查看并在 Canvas 上画线