操作栏和导航抽屉 - 使用活动/片段滑动操作栏

时间:2013-08-31 14:49:39

标签: android navigation android-actionbar drawer

我正在使用导航抽屉,如here: Android Example所示。目前,Actionbar是静态的,当抽屉打开/关闭时它不会移动(只有它的标题更改)。我该如何应用这种效果:

Image

我希望整个ActionBar随Sliding片段一起移动。并且ActionBar上的名称和按钮保持不变。请告诉我你需要看的代码。

另外,问题2:

当你使用DrawerLayout时,你在xml中包含FrameLayout(用于content_frame)和ListView(你添加导航设置的地方......在那个抽屉里,你可以修改布局,这样你就可以不仅可以添加ListView,还可以添加其他视图吗?在ListView的顶部或底部?像这样:

enter image description here

我想添加未链接的ImageView(不是超链接,只是图像)和附加TextViews(用于说明)。

4 个答案:

答案 0 :(得分:9)

考虑使用SlidingMenu中的GitHub库。它具有强大的灵活性,您也可以随心所欲地使用它。您只需调用:

即可滑动操作栏
setSlidingActionBarEnabled(true);

https://github.com/jfeinstein10/SlidingMenu

答案 1 :(得分:2)

第二点:当然可以。 NavigationDrawer不仅限于使用ListView。只需使用像LinearLayout这样的ViewGroup作为DrawerLayout的第二个子项,并放入您想要的任何视图中。 但请记住在LinearLayout的规范中添加此行:

<LinearLayout
     ...
     android:layout_gravity="start"
     ...>

     <TextView..../>
     <ImageView...../>
<LinearLayout/>

答案 2 :(得分:2)

我认为你可以使用这种布局来实现抽屉列表视图顶部的一些视图。

<!-- The main content view -->

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>
<!-- The navigation drawer -->

<RelativeLayout
    android:id="@+id/drawerLeft"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="start"
    android:background="#191919" >

    <EditText
        android:id="@+id/edit_Search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FC3A3B"
        android:hint="  Search"
        android:padding="10dp"
        android:textColor="#ffffff"
        android:textColorHint="#ffffff" >

        <requestFocus />
    </EditText>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/edit_Search"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="5dp"
        android:layout_marginRight="20dp"
        android:src="@drawable/icon_small_search" />

    <ListView
        android:id="@+id/drawer_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/edit_Search"
        android:background="#191919"
        android:choiceMode="singleChoice"
        android:divider="#000000"
        android:dividerHeight="1dp" >
    </ListView>
</RelativeLayout>

答案 3 :(得分:1)

我认为这就是你要找的东西。这是您所引用示例的示例源代码中的代码。

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
    mDrawerLayout, /* DrawerLayout object */
    R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
    R.string.drawer_open, /* "open drawer" description for accessibility */
    R.string.drawer_close /* "close drawer" description for accessibility */
    ) {
        public void onDrawerClosed(View view) {
            getActionBar().setTitle(mTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }

        public void onDrawerOpened(View drawerView) {
            getActionBar().setTitle(mDrawerTitle);
            invalidateOptionsMenu(); // creates call to
                                        // onPrepareOptionsMenu()
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
相关问题