带工具栏的Android抽屉菜单

时间:2015-04-12 22:40:30

标签: java android

是否可以在菜单列表项目上方的抽屉菜单中设置第二个工具栏?我希望工具栏能够粘贴滑动菜单并与原始内容和工具栏重叠。 我的代码:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
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:context=".MainActivity">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!-- our toolbar -->
    <include layout="@layout/toolbar" />

    <!-- frameLayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

<!-- listView to display slider menu -->
<!-- I want to add here a toolbar that will stick to my slide menu-->
<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/white"
    android:choiceMode="singleChoice"
    android:divider="@null"/>

1 个答案:

答案 0 :(得分:2)

我假设你想在滑动抽屉菜单中放置一个工具栏以获得一个漂亮的标题?

你可以做的一件事,不一定是使用工具栏,但你可以创建一个单独的布局文件,如下所示:

<?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" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:background="@color/blue"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/tvENumData"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_margin="8dp"
            android:text="Options"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#fff"
            android:textSize="20sp"
            android:textStyle="bold" />

    </RelativeLayout>

</LinearLayout>

然后将其作为标题添加到滑动菜单中:

ViewGroup header = (ViewGroup) inflater.inflate(R.layout.drawer_title, container, false);
mDrawerListView.addHeaderView(header);

这是结果:

enter image description here

有了这个,你可以为你的滑动菜单设置一个非常漂亮,易于定制的标题,如果这不是你想要的并且真的想要使用工具栏那么我很抱歉建议这个,我只是想帮忙。