首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >顶部ActionBar上的安卓导航抽屉

顶部ActionBar上的安卓导航抽屉
EN

Stack Overflow用户
提问于 2014-04-25 21:44:14
回答 4查看 66.5K关注 0票数 51

这是我的主要活动的布局:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout ...>
    <RelativeLayout android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
        ...
    </RelativeLayout>
    <fragment android:name="com...." 
        android:layout_gravity="start" 
        android:id="@id/navigation" 
        android:layout_width="@dimen/navigation_menu_width" 
        android:layout_height="fill_parent" />
</android.support.v4.widget.DrawerLayout>

关于stackoverflow的其他一些问题也是类似的,比如this question,但是所有的答案都推荐使用滑动菜单库。但这个应用程序他们仍然使用android.support.v4.widget.DrawerLayout,并且他们成功了。不要问我是怎么知道他们使用标准导航抽屉的,但我对此很确定。

会非常感谢你的帮助。

这里是最终的解决方案:非常感谢@Peter Cai,它工作得很完美。https://github.com/lemycanh/DrawerOnTopActionBar

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-10-03 15:32:27

我从https://github.com/jfeinstein10/SlidingMenu学到了一个小“技巧”来实现你所需要的效果。

您只需要删除窗口装饰视图的第一个子级,并将第一个子级添加到您的抽屉的内容视图中。之后,您只需将您的抽屉添加到窗口的装饰视图中。

下面是一些详细的步骤,你可以这样做。

首先,创建一个名为"decor.xml“或任何您喜欢的xml。只把DrawerLayout和抽屉放进去。下面的"FrameLayout“只是一个容器。我们将使用它来包装您活动的内容。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout ...>
    <FrameLayout android:id="@+id/container"
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/>
    <fragment android:name="com...." 
        android:layout_gravity="start" 
        android:id="@id/navigation" 
        android:layout_width="@dimen/navigation_menu_width" 
        android:layout_height="fill_parent" />
</android.support.v4.widget.DrawerLayout>

然后删除主布局中的DrawerLayout。现在,主活动的布局应该如下所示

代码语言:javascript
复制
<RelativeLayout android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    ...
</RelativeLayout>

我们假设主活动的布局名为"main.xml“。

在您的MainActivity中,按如下方式编写:

代码语言:javascript
复制
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Inflate the "decor.xml"
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    DrawerLayout drawer = (DrawerLayout) inflater.inflate(R.layout.decor, null); // "null" is important.

    // HACK: "steal" the first child of decor view
    ViewGroup decor = (ViewGroup) getWindow().getDecorView();
    View child = decor.getChildAt(0);
    decor.removeView(child);
    FrameLayout container = (FrameLayout) drawer.findViewById(R.id.container); // This is the container we defined just now.
    container.addView(child);

    // Make the drawer replace the first child
    decor.addView(drawer);

    // Do what you want to do.......

}

现在你有了一个可以在ActionBar上滑动的DrawerLayout。但是你可能会发现它被状态栏覆盖了。您可能需要在抽屉中添加一个paddingTop以修复此问题。

票数 51
EN

Stack Overflow用户

发布于 2014-04-25 21:48:59

更新:如何使用导航抽屉覆盖actionbar。(使用新的工具栏)在build.gradle的依赖项中使用这些

代码语言:javascript
复制
compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:support-v4:21.0.0'

这是您的抽屉布局

代码语言:javascript
复制
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<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">
    <LinearLayout
        android:id="@+id/layout_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <include layout="@layout/toolbar"/>
    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"/>

    </LinearLayout>
    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/list_background"
         />

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

在布局文件夹中创建新的toolbar.xml文件。

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />

转到展开导航抽屉的活动。并将此添加到SetContentView()之后

代码语言:javascript
复制
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

别忘了在values文件夹中扩展你的主题NoActionBar。

代码语言:javascript
复制
<style name="Theme.Whtsnxt" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
    <!-- colorPrimary is used for the default action bar background -->
    <item name="windowActionModeOverlay">true</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="colorPrimary">@color/splashscreen</item>
    <item name="colorPrimaryDark">@color/holo_blue_light</item>

    <item name="android:windowBackground">@color/white</item>
    <item name="android:colorBackground">@color/white</item>

</style>
票数 34
EN

Stack Overflow用户

发布于 2017-11-28 08:30:32

如果您不想使用lib或此hack:

将"Theme.AppCompat.NoActionBar"

  • Move DrawerLayout的第一个元素扩展成一个工具栏,并将其添加到
  1. LinearLayout中。

Activity在setContentView之后添加以下行

工具栏((SetSupportActionBar) findViewById(R.id.toolbar));

  • now it )应该可以工作。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23294954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档