活动布局上的导航抽屉布局

时间:2015-09-24 05:03:19

标签: android

我想创建一个位于主要活动之上的导航抽屉。但是这里有代码

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

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".invento">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
        </RelativeLayout>

<fragment
    android:layout_width="@dimen/nav_drawerWidth"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:layout="@layout/fragment_navigation_drawer"
    android:name="com.invento.defcomm.invento16.navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

我正在操作栏下方的抽屉,而不是完全重叠整个活动。请帮我解决这个问题并指出我做错了什么?感谢....

2 个答案:

答案 0 :(得分:0)

试试这个,

<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"
android:fitsSystemWindows="true">

<!-- The main content view -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <!-- Toolbar instead of ActionBar so the drawer can slide on top -->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/abc_action_bar_default_height_material"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:titleTextAppearance="?android:attr/textAppearanceMedium" />

    <!-- Real content goes here -->
    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>


<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:menu="@menu/drawer" />

答案 1 :(得分:0)

为什么不将默认操作栏更改为工具栏。这样,您可以将工具栏包含在布局中,并将导航抽屉保留在工具栏的顶部。

这是我用来添加工具栏的代码:

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_actionbar"
        style="@style/ToolBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="@dimen/abc_action_bar_default_height_material"/>

</LinearLayout>

然后我在上面的布局中包含上面的xml。

 <include
    android:id="@+id/toolbar"
    layout="@layout/toolbar_default"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="false"/>

所以在你的xml中,你可以简单地这样做:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".invento">

 <include
    android:id="@+id/toolbar"
    layout="@layout/toolbar_default"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="false"/>

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

<fragment
    android:layout_width="@dimen/nav_drawerWidth"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:layout_alignParentTop="true"
    app:layout="@layout/fragment_navigation_drawer"
    android:name="com.invento.defcomm.invento16.navigation_drawer"
    tools:layout="@layout/fragment_navigation_drawer" />

</RelativeLayout>

在您的活动中,将工具栏指定为您的ActionBar:

mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
mToolbar.setLogo(getResources().getDrawable(R.drawable.ic_launcher)); // Your Logo
mToolbar.setTitle("Title"); // Your Title
setSupportActionBar(mToolbar);