这是我使用/写的代码.
XML DrawerLayout:
<android.support.v4.widget.DrawerLayout android:id="@+id/drawer_layout" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <include layout="@layout/toolbar" /> </FrameLayout> <com.andrewq.planets.util.ScrimInsetsFrameLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/linearLayout" android:layout_width="304dp" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:insetForeground="#4000"> <ListView android:id="@+id/left_drawer" android:layout_width="match_parent" android:layout_height="match_parent" android:choiceMode="singleChoice" /> </com.andrewq.planets.util.ScrimInsetsFrameLayout> </android.support.v4.widget.DrawerLayout>
ScrimInsetsFrameLayout.java:
package com.andrewq.planets.util; /* * 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. */ import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.support.v4.view.ViewCompat; import android.util.AttributeSet; import android.widget.FrameLayout; import com.andrewq.planets.R; /** * 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,AttributeSet attrs,int defStyle) { super(context,defStyle); init(context,defStyle); } private void init(Context context,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,width,mInsets.top); mInsetForeground.setBounds(mTempRect); mInsetForeground.draw(canvas); // Bottom mTempRect.set(0,height - mInsets.bottom,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,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); } }
最后,这里是我的styles.xml for values-v21:
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="AppThemeNavDrawer" parent="Theme.AppCompat.NoActionBar"> <item name="colorAccent">#F8F8F8</item> <item name="android:windowTranslucentStatus">true</item> <item name="windowActionBar">false</item> <item name="windowActionModeOverlay">true</item> <item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:statusBarColor">@android:color/transparent</item> </style> </resources>
我看过2014年的I / O应用源代码以及this的问题,我不知道有什么不同.
这是一个屏幕截图,我到目前为止减去状态栏下的抽屉:
我有一切工作完美,这是我需要做的最后一件事.帮助非常感谢!
编辑:
为了弄清楚,我想像在大多数Google Apps和Google Now中一样,将状态栏中的图像着色.
解决方法
我创建了一个MaterialDrawer(遵循Android材料设计指南),它实现了所有这一切,并为您处理一切.在这里阅读更多:https://github.com/mikepenz/MaterialDrawer/
如果你想自己创建它,你总是要决定哪个是你最需要支持的API,和/或者你必须分开你的样式.
所以要启用半透明状态栏,您必须至少在API v19上,或者为v19创建一个分离样式-v19
这样会看起来像这样
<style name="YourTheme.TranslucentStatus" parent="Theme.AppCompat.Light.NoActionBar"> <item name="android:windowTranslucentStatus">true</item> </style>
所以现在这将会将您的完整布局移到状态栏下方.在几乎所有情况下,您现在都需要在抽屉内容和普通视图内容的顶部添加填充.
你可以通过添加24dp填充来做到这一点.
这不是一个很好的实现.因此,使用Google IO 2014应用程序中使用的ScrimInsetsLayout有一种不同的方法. https://github.com/google/iosched/blob/master/android/src/main/java/com/google/samples/apps/iosched/ui/widget/ScrimInsetsFrameLayout.java
这将是您的内容布局,您可以设置状态栏的颜色.您可以在这里找到有关如何使用它的详细说明:https://stackoverflow.com/a/26932228
它需要一些时间来适应风格和/或ScrimInsetsLayout.
编辑:
关于如何以编程方式处理这个更复杂的示例:
if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) { //enable translucent statusbar via flags setTranslucentStatusFlag(true); } if (Build.VERSION.SDK_INT >= 19) { mActivity.getWindow().getDecorView().setsystemUIVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); } if (Build.VERSION.SDK_INT >= 21) { //we don't need the translucent flag this is handled by the theme setTranslucentStatusFlag(false); //set the statusbarcolor transparent to remove the black shadow mActivity.getWindow().setStatusBarColor(Color.TRANSPARENT); } //add a padding to the content of the drawer (25dp on devices starting with api v19) mDrawerContentRoot.setPadding(0,mActivity.getResources().getDimensionPixelSize(R.dimen.tool_bar_top_padding),0); // define the statusBarColor mDrawerContentRoot.setInsetForeground(mStatusBarColor); private void setTranslucentStatusFlag(boolean on) { if (Build.VERSION.SDK_INT >= 19) { Window win = mActivity.getWindow(); WindowManager.LayoutParams winParams = win.getAttributes(); final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS; if (on) { winParams.flags |= bits; } else { winParams.flags &= ~bits; } win.setAttributes(winParams); } }
EDIT2:
解决此问题的完整解决方案是清理项目中的所有布局.一些布局和风格的组合引起了麻烦.
完整的更改可以在这个拉动请求中找到:
https://github.com/Andrew-Quebe/Planets-Gradle/commit/83e28c09253af6e807b6f4e94baca8fbca3fc7c8