【问题标题】:Actionbar and Navigation drawer - slide Actionbar with Activity/Fragment操作栏和导航抽屉 - 使用 Activity/Fragment 滑动操作栏
【发布时间】:2013-09-04 03:45:07
【问题描述】:

我正在使用导航抽屉,如 here: Android Example 所示。目前,Actionbar 是静态的,抽屉打开/关闭时它不会移动(只是它的标题发生了变化)。如何应用此效果:

我希望整个 ActionBar 与 Sliding 片段一起移动。并且 ActionBar 上的 Name 和 Buttons 保持原样。请告诉我您需要查看哪些代码。

另外,问题 2:

当您使用DrawerLayout 时,您在 xml 中包含 FrameLayout(用于 content_frame)和 ListView(您在其中添加导航设置...在该抽屉中,您可以修改布局以便添加不只是 ListView,还有其他 View?在 ListView 的顶部还是底部?像这样的:

我想添加 ImageView(不是超链接,只是 Image)和 Additional TextViews(用于说明)不是超链接。

【问题讨论】:

  • 看看here
  • 我会将它添加到我的代码中。另请参阅上面我的编辑中的问题 2。
  • @KickAss:如果可能的话,请发布NavigationDrawer的自定义代码,这也会对其他人有所帮助。
  • 如何定制?我现在使用 ActionBarSherlock 和 SlidingMenu 库。这两者都是向后兼容的,并且很容易实现和定制。

标签: android navigation android-actionbar drawer


【解决方案1】:

考虑使用来自GitHubSlidingMenu 库。它非常灵活,你可以用它做任何事情。您只需调用即可滑动操作栏:

setSlidingActionBarEnabled(true);

https://github.com/jfeinstein10/SlidingMenu

【讨论】:

  • 哇,如果我 2 天前知道这一点,我就不会花 2 天时间来尝试使 Android 示例工作并对其进行自定义:(
  • 下载此代码后..此代码包含错误。请解决它。我很沮丧。
  • 其实是需要abs库的。那是什么 ?请解决它。拜托了
  • 我尝试使用滑动菜单。但是当滑块打开时,主视图不会像其他应用程序那样褪色。
  • @ShashankAgarwal 确保添加这些行:menu.setShadowWidthRes(R.dimen.shadow_width); menu.setShadowDrawable(R.drawable.shadow); menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

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

    <!-- 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>
    

    【讨论】:

      【解决方案4】:

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

          // 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多