如何在状态栏下方放置导航抽屉标题?

时间:2016-08-30 11:54:01

标签: android

我希望导航抽屉的标题位于状态栏下方。在搜索解决方案时,每个结果都是关于如何将其置于状态栏下。我当然试图“反转”theese解决方案,将标题放在下面。但它没有奏效,我正在Samsung Galaxy S7 API 23

进行测试

现在它看起来像这样:

这是我的布局,包含DrawerLayout,NavigationView:

<?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:background="#00ffff"
android:fitsSystemWindows="true"
tools:openDrawer="start">


  <include
    android:id="@+id/toolbar_drawer_layout"
    layout="@layout/navdrawer_toolbar"
    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:background="@color/arion_darkgray"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/navdrawer_header"
    app:itemBackground="@drawable/nav_drawer_selector"
    app:itemIconTint="#fff"
    app:itemTextColor="#fff"
    app:menu="@menu/navdrawer_menu"
    app:theme="@style/custom_navDrawer_style" />

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

工具栏布局:

<?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=".MyHorseActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.Toolbar
        android:id="@+id/myhorse_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/arion_darkblue">

        <RelativeLayout
            android:id="@+id/myhorse_toolbar_innerlayout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:paddingRight="70dp"
                android:text="My Horse"
                android:textColor="#fff"
                android:textSize="24sp" />

            <ImageView
                android:backgroundTint="#ff0000"
                android:layout_width="15dp"
                android:layout_height="15dp"
                android:layout_alignParentRight="true"
                android:layout_centerInParent="true"
                android:layout_marginRight="10dp"
                android:background="@drawable/circle_shape" />
        </RelativeLayout>

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

    <include
        layout="@layout/divider_lightblue"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_alignParentBottom="true">

    </include>


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

<include
    android:id="@+id/activity"
    layout="@layout/myhorse_activity_" />

在我从向导

创建NavigationDrawer类后,Android Studio会自动生成大部分内容

2 个答案:

答案 0 :(得分:1)

你还没有完全放置xml,所以我不知道你的xml有什么不对,但是如果你想要navigation drawer action bar以下那么就放drawerlayout toolbar 1}}低于<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--Start of the Toolbar and its items --> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" /> <android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"></LinearLayout> <!--Used for displaying Navigation Drawer slide--> <LinearLayout android:id="@+id/navigation_drawer" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:orientation="vertical"> <FrameLayout android:id="@+id/navigation_container" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </android.support.v4.widget.DrawerLayout> </LinearLayout> ,如下

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

    <include
        android:id="@+id/toolbar_drawer_layout"
        layout="@layout/navdrawer_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <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:background="#00ffff"
        android:fitsSystemWindows="true"
        tools:openDrawer="start">

        <include
            android:id="@+id/activity"
            layout="@layout/myhorse_activity_" />

        <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:background="@color/arion_darkgray"
            android:fitsSystemWindows="true"
            app:headerLayout="@layout/navdrawer_header"
            app:itemBackground="@drawable/nav_drawer_selector"
            app:itemIconTint="#fff"
            app:itemTextColor="#fff"
            app:menu="@menu/navdrawer_menu"
            app:theme="@style/custom_navDrawer_style" />

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

根据您编辑的问题,drawerlayout xml就像

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/myhorse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/arion_darkblue">

            <RelativeLayout
                android:id="@+id/myhorse_toolbar_innerlayout"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:paddingRight="70dp"
                    android:text="My Horse"
                    android:textColor="#fff"
                    android:textSize="24sp" />

                <ImageView
                    android:layout_width="15dp"
                    android:layout_height="15dp"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/circle_shape"
                    android:backgroundTint="#ff0000" />
            </RelativeLayout>

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

        <include
            layout="@layout/divider_lightblue"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignParentBottom="true" />
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

和工具栏布局类似

<body>

答案 1 :(得分:0)

我使用下面的xml文件来实现您的要求。但是这样,抽屉就会在&#34;状态栏下面打开&#34;和&#34;工具栏&#34;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<include xmlns:android="http://schemas.android.com/apk/res/android" layout="@layout/toolbar" />

<android.support.v4.widget.DrawerLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:elevation="5dp">

    <FrameLayout
        android:id = "@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        </FrameLayout>

    <ListView
        android:background="#ffffff"
        android:id = "@+id/drawerList"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:elevation="5dp"></ListView>

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

</LinearLayout>