如何使用DrawerLayout在ActionBar /工具栏上和状态栏下显示?

Chr*_*nes 391 android android-appcompat navigation-drawer

我在新的材料设计Side Nav规范中看到,您可以在操作栏上和状态栏后面显示抽屉.我该如何实现呢?

Chr*_*nes 526

框架和支持库中的新功能允许这一点.有三个'拼图':

  1. 使用工具栏,您可以将操作栏嵌入到视图层次结构中.
  2. 制作DrawerLayout fitsSystemWindows,使其布置在系统栏后面.
  3. 禁用Theme.Material正常状态栏着色,以便DrawerLayout可以在那里绘制.

我假设您将使用新的appcompat.

首先,您的布局应如下所示:

<!-- The important thing to note here is the added fitSystemWindows -->
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

        <!-- We use a Toolbar so that our drawer can be displayed
             in front of the action bar -->
        <android.support.v7.widget.Toolbar  
            android:id="@+id/my_awesome_toolbar"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:minHeight="?attr/actionBarSize"
            android:background="?attr/colorPrimary" />

        <!-- The rest of your content view -->

    </LinearLayout>

    <!-- Your drawer view. This can be any view, LinearLayout
         is just an example. As we have set fitSystemWindows=true
         this will be displayed under the status bar. -->
    <LinearLayout
        android:layout_width="304dp"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:fitsSystemWindows="true">

        <!-- Your drawer content -->

    </LinearLayout>

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

然后在你的Activity/Fragment中:

public void onCreate(Bundled savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Your normal setup. Blah blah ...

    // As we're using a Toolbar, we should retrieve it and set it
    // to be our ActionBar
    Toolbar toolbar = (...) findViewById(R.id.my_awesome_toolbar);
    setSupportActionBar(toolbar);

    // Now retrieve the DrawerLayout so that we can set the status bar color.
    // This only takes effect on Lollipop, or when using translucentStatusBar
    // on KitKat.
    DrawerLayout drawerLayout = (...) findViewById(R.id.my_drawer_layout);
    drawerLayout.setStatusBarBackgroundColor(yourChosenColor);
}
Run Code Online (Sandbox Code Playgroud)

然后,您需要确保在状态栏后面可以看到DrawerLayout.你通过改变你的值-v21主题来做到这一点:

值-V21 /的themes.xml

<style name="Theme.MyApp" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

注意:如果使用a <fragment android:name="fragments.NavigationDrawerFragment">代替

<LinearLayout
    android:layout_width="304dp"
    android:layout_height="match_parent"
    android:layout_gravity="left|start"
    android:fitsSystemWindows="true">

    <!-- Your drawer content -->

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

实际布局,如果您调用fitsSystemWindows(boolean)onCreateView方法返回的视图,将实现所需的效果.

@Override
public View onCreateView(LayoutInflater inflater, 
                         ViewGroup container,
                         Bundle savedInstanceState) {
    View mDrawerListView = inflater.inflate(
        R.layout.fragment_navigation_drawer, container, false);
    mDrawerListView.setFitsSystemWindows(true);
    return mDrawerListView;
}
Run Code Online (Sandbox Code Playgroud)

  • 为了通过状态栏可以看到抽屉可见的效果,我最终在DrawerLayout上将android:fitsSystemWindows设置为false,在我的主要内容布局(包含工具栏的布局)上将android:fitsSystemWindows设置为true,并添加<item name ="android:windowTranslucentStatus"> true </ item>到我的主题 - 在Lollipop上 (26认同)
  • 根本无法让这个工作.使用上面的代码在新的演示应用程序中,结果看起来与@ScootrNova描述的类似.请参阅此屏幕截图:http://imgur.com/QLk3syt (11认同)
  • 这个答案是不完整的.您还需要将导航抽屉包装在ScrimLayout中.请参见http://stackoverflow.com/a/26926998/174149 (8认同)
  • 看起来你实际上需要在抽屉内容的布局根目录中使用否定的`layout_marginTop`.否则,您的抽屉内容的布局背景将绘制在状态栏的顶部,其他所有内容都将被按下.然而,这似乎是一个有点粗略的解决方案,据我所知,没有?attr/statusBarSize或Android提供的任何东西. (6认同)
  • 是的,'DrawerLayout`需要将它放在系统栏后面.然后,您需要在抽屉视图上进行设置,以便"DrawerLayout"在窗口内插入它. (3认同)
  • @ bvx89我找到的唯一解决方案是将DrawerLayout的背景颜色设置为所需的颜色.我希望这可以帮助你. (3认同)
  • 大多数活动需要ActionBar Theme而不是NoActionBar.我需要在ActionBar/NoActionBar主题上设置全局Buttons/TextView/EditText样式.但风格不能有两个父母.你的建议是什么? (2认同)
  • @ Jakob的答案有效,但`colorPrimary`属性不再适用.我希望它看起来像[this](http://material-design.storage.googleapis.com/publish/v_2/material_ext_publish/0Bx4BSt6jniD7c2dhcFRZdzBPWnM/patterns_navdrawer_elevation1.png),但它看起来像这个[this](https:// drive.google.com/file/d/0Bz-UGI12cm16eTZiYkRtaUZ4dnc/view?usp=sharing)(它应该是深红色的).有什么建议? (2认同)
  • @PaulBurke对我来说`windowTranslucentStatus`导致工具栏被状态栏透支. (2认同)

Suy*_*ash 137

编辑:新的设计支持库支持此功能,不再需要以前的方法.

现在可以使用新的Android设计支持库来实现.

您可以看到Chris Banes 的Cheesesquare示例应用程序,该应用程序演示了所有新功能.


以前的方法:

由于没有公布完整的解决方案,这是我达到预期结果的方式.

首先在项目中包含ScrimInsetsFrameLayout.

/*
* Copyright 2014 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* A layout that draws something in the insets passed to 
* {@link #fitSystemWindows(Rect)}, i.e. the area above UI chrome
* (status and navigation bars, overlay action bars).
*/
public class ScrimInsetsFrameLayout extends FrameLayout {
    private Drawable mInsetForeground;

    private Rect mInsets;
    private Rect mTempRect = new Rect();
    private OnInsetsCallback mOnInsetsCallback;

    public ScrimInsetsFrameLayout(Context context) {
        super(context);
        init(context, null, 0);
    }

    public ScrimInsetsFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    public ScrimInsetsFrameLayout(
        Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int defStyle) {
        final TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.ScrimInsetsView, defStyle, 0);
        if (a == null) {
            return;
        }
        mInsetForeground = a.getDrawable(
            R.styleable.ScrimInsetsView_insetForeground);
        a.recycle();

        setWillNotDraw(true);
    }

    @Override
    protected boolean fitSystemWindows(Rect insets) {
        mInsets = new Rect(insets);
        setWillNotDraw(mInsetForeground == null);
        ViewCompat.postInvalidateOnAnimation(this);
        if (mOnInsetsCallback != null) {
            mOnInsetsCallback.onInsetsChanged(insets);
        }
        return true; // consume insets
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);

        int width = getWidth();
        int height = getHeight();
        if (mInsets != null && mInsetForeground != null) {
            int sc = canvas.save();
            canvas.translate(getScrollX(), getScrollY());

            // Top
            mTempRect.set(0, 0, width, mInsets.top);
            mInsetForeground.setBounds(mTempRect);
            mInsetForeground.draw(canvas);

            // Bottom
            mTempRect.set(0, height - mInsets.bottom, width, height);
            mInsetForeground.setBounds(mTempRect);
            mInsetForeground.draw(canvas);

            // Left
            mTempRect.set(
                0, 
                mInsets.top, 
                mInsets.left, 
                height - mInsets.bottom);
            mInsetForeground.setBounds(mTempRect);
            mInsetForeground.draw(canvas);

            // Right
            mTempRect.set(
                width - mInsets.right, 
                mInsets.top, width, 
                height - mInsets.bottom);
            mInsetForeground.setBounds(mTempRect);
            mInsetForeground.draw(canvas);

            canvas.restoreToCount(sc);
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (mInsetForeground != null) {
            mInsetForeground.setCallback(this);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (mInsetForeground != null) {
            mInsetForeground.setCallback(null);
        }
    }

    /**
     * Allows the calling container to specify a callback for custom 
     * processing when insets change (i.e. when {@link #fitSystemWindows(Rect)}
     * is called. This is useful for setting padding on UI elements 
     * based on UI chrome insets (e.g. a Google Map or a ListView). 
     * When using with ListView or GridView, remember to set
     * clipToPadding to false.
     */
    public void setOnInsetsCallback(OnInsetsCallback onInsetsCallback) {
        mOnInsetsCallback = onInsetsCallback;
    }

    public static interface OnInsetsCallback {
        public void onInsetsChanged(Rect insets);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后创建一个样式,以便insetForeground可以设置.

值/ attrs.xml

<declare-styleable name="ScrimInsetsView">
    <attr name="insetForeground" format="reference|color" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

更新您的活动的xml文件,并确保android:fitsSystemWindows在两者DrawerLayout以及ScrimInsetsFrameLayout.

布局/ activity_main.xml中

<android.support.v4.widget.DrawerLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

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

        <!-- Your main content -->

    </LinearLayout>

    <!-- The navigation drawer -->
    <com.example.app.util.ScrimInsetsFrameLayout 
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/scrimInsetsFrameLayout"
        android:layout_width="320dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/white"
        android:elevation="10dp"
        android:fitsSystemWindows="true"
        app:insetForeground="#4000">

        <!-- Your drawer content -->

    </com.example.app.util.ScrimInsetsFrameLayout>

</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

在活动的onCreate方法内,在抽屉布局上设置状态栏背景颜色.

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // ...

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    mDrawerLayout.setStatusBarBackgroundColor(
        getResources().getColor(R.color.primary_dark));
}
Run Code Online (Sandbox Code Playgroud)

最后更新您的应用主题,以便它DrawerLayout位于状态栏后面.

值-V21/styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

结果:

  • 这对我来说是最好的.我花了两天时间让这件事情发挥作用,这就是其中之一.这应该标记为答案.非常感谢. (10认同)
  • 我花了三天时间才接受目前除了使用这个额外的布局包裹抽屉片段之外别无他法.否则状态栏将为灰色(第一个孩子的背景颜色),或者抽屉将显示在状态栏下方. (5认同)
  • 这是最完美的答案.我已经在从4.X到5.X的各种Android设备中实现并测试了它,并且可以确认它完美无缺.非常感谢. (3认同)
  • 我没有使用appcompat,这不起作用.:(有没有任何指南,如果没有appcompat这个工作? (2认同)
  • 正如我所说,它完美无缺,但我有一点问题.导航抽屉的活动具有透明状态栏,但所有其他活动都从状态栏中丢失了"primaryDark"颜色.怎么回来? (2认同)

reV*_*rse 95

随着最新的Android支持库(rev 22.2.0)的发布,我们有了一个设计支持库,并且作为一个名为NavigationView的新视图的一部分.因此,ScrimInsetsFrameLayout我们只使用这个视图而不是自己做所有其他事情,而是为我们完成所有事情.

步骤1

添加Design Support Library到您的build.gradle文件

dependencies {
    // Other dependencies like appcompat
    compile 'com.android.support:design:22.2.0'
}
Run Code Online (Sandbox Code Playgroud)

第2步

添加NavigationView到您的DrawerLayout:

<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"> <!-- this is important -->

     <!-- Your contents -->

     <android.support.design.widget.NavigationView
         android:id="@+id/navigation"
         android:layout_width="wrap_content"
         android:layout_height="match_parent"
         android:layout_gravity="start"
         app:menu="@menu/navigation_items" /> <!-- The items to display -->
 </android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)

第3步

创建一个新的菜单资源/res/menu并添加您想要显示的项目和图标:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_action_home"
            android:title="Home" />
        <item
            android:id="@+id/nav_example_item_1"
            android:icon="@drawable/ic_action_dashboard"
            android:title="Example Item #1" />
    </group>

    <item android:title="Sub items">
        <menu>
            <item
                android:id="@+id/nav_example_sub_item_1"
                android:title="Example Sub Item #1" />
        </menu>
    </item>

</menu>
Run Code Online (Sandbox Code Playgroud)

第4步

初始化NavigationView并处理点击事件:

public class MainActivity extends AppCompatActivity {

    NavigationView mNavigationView;
    DrawerLayout mDrawerLayout;

    // Other stuff

    private void init() {
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mNavigationView = (NavigationView) findViewById(R.id.navigation_view);
        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(MenuItem menuItem) {
                mDrawerLayout.closeDrawers();
                menuItem.setChecked(true);
                switch (menuItem.getItemId()) {
                    case R.id.nav_home:
                        // TODO - Do something
                        break;
                    // TODO - Handle other items
                }
                return true;
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

第5步

一定要设置android:windowDrawsSystemBarBackgroundsandroid:statusBarColorvalues-v21否则你的抽屉那倒显示"下的"状态栏

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Other attributes like colorPrimary, colorAccent etc. -->
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)

可选步骤

向NavigationView添加标题.为此,只需创建一个新布局并添加app:headerLayout="@layout/my_header_layout"到NavigationView.

结果

图片显示导航视图

笔记

  • 高亮颜色使用经由所述定义的颜色colorPrimary属性
  • 列表项使用的颜色通过定义的textColorPrimary属性
  • 图标使用颜色通过定义的textColorSecondary属性

您还可以查看Chris Banes示例应用程序,其中突出显示了NavigationView以及属于Design Support Library的其他新视图(如FloatingActionButton,TextInputLayout,Snackbar,TabLayout等).

  • 此方法仍然生成ActionBar后面的NavigationView. (2认同)

Nic*_*lle 6

使它工作,在values-v21样式或主题xml需要使用此属性:

<item name="android:windowTranslucentStatus">true</item>

那就是魔术!

  • 这使我的操作栏也显示在状态栏后面. (3认同)
  • 海报和回答者是设计AppCompat的Google员工,所以我很确定他在写内容时知道自己在做什么. (2认同)

Vip*_*ahu 6

以上所有方法都是正确的,可能有效.我按照上面的指南创建了一个工作演示,并在2.x到5.x上进行了测试

你可以从Github克隆

重要的是在主要活动中

toolbar = (Toolbar) findViewById(R.id.toolbar);
res = this.getResources();

this.setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
    ScrimInsetsFrameLayout scrimInsetsFrameLayout = (ScrimInsetsFrameLayout)
            findViewById(R.id.linearLayout);
    scrimInsetsFrameLayout.setOnInsetsCallback(this);
} 
Run Code Online (Sandbox Code Playgroud)

和回电

@Override
public void onInsetsChanged(Rect insets) {
    Toolbar toolbar = this.toolbar;
    ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)
        toolbar.getLayoutParams();
    lp.topMargin = insets.top;
    int top = insets.top;
    insets.top += toolbar.getHeight();
    toolbar.setLayoutParams(lp);
    insets.top = top; // revert
}
Run Code Online (Sandbox Code Playgroud)

绝对是V21的主题是神奇的

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- API 21 theme customizations can go here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/accent_material_light</item>
    <item name="windowActionModeOverlay">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)

和ScrimInsetsFrameLayout

现在,使用新的Design Support库可以更轻松

compile 'com.android.support:design:22.2.0'
Run Code Online (Sandbox Code Playgroud)

克隆自@Chris Banes https://github.com/chrisbanes/cheesesquare