我正在开发Android项目,我正在实现导航抽屉.我正在阅读新的材料设计规范和材料设计清单.
规范说滑出窗格应该浮动在其他所有内容之上,包括状态栏,并且在状态栏上是半透明的.
我的导航面板位于状态栏上,但没有任何透明度.我按照Google开发者博客网站上的建议点击了这篇SO帖子中的代码,上面的链接如何使用DrawerLayout显示在ActionBar /工具栏上和状态栏下?.
下面是我的XML布局
以下是我的应用主题
以下是我的应用程序v21主题
下面是我的onCreate方法
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); setSupportActionBar(toolbar); mDrawerLayout = (DrawerLayout)findViewById(R.id.my_drawer_layout); mDrawerList = (ListView)findViewById(R.id.left_drawer); mDrawerLayout.setStatusBarBackgroundColor( getResources().getColor(R.color.appPrimaryColourDark)); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout); linearLayout.setElevation(30); }
下面是我的导航抽屉的屏幕截图,显示顶部不是半透明的
您需要将导航抽屉布局包装在一个ScrimLayout
.
A ScrimLayout
基本上在导航抽屉布局上绘制一个半透明矩形.要检索插入的大小,只需覆盖fitSystemWindows
您的ScrimLayout
.
@Override protected boolean fitSystemWindows(Rect insets) { mInsets = new Rect(insets); return true; }
稍后覆盖onDraw
以绘制半透明矩形.
一个例子实现可以在谷歌IO应用源被发现. 在这里你可以看到它在布局xml中的使用方式.
您需要做的就是为状态栏使用一些半透明颜色.将这些行添加到v21主题:
<item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:statusBarColor">@color/desired_color</item>
不要忘记color
(资源)必须始终采用格式#AARRGGBB
.这意味着颜色也将包含alpha值.
您的状态栏背景为白色,即抽屉的背景LinearLayout
.为什么?你是fitsSystemWindows="true"
你DrawerLayout
和你内部的设置LinearLayout
.这会导致您在状态栏后面LinearLayout
展开(这是透明的).因此,使状态栏的抽屉部分的背景变白.
如果你不想让你的抽屉状态栏后面延伸(想拥有整个状态栏半透明背景),你可以做两件事情:
1)您可以简单地从您LinearLayout
的内容中删除任何背景值,并为其中的内容背景着色.要么
2)你可以fitsSystemWindows="true"
从你的LinearLayout
.我认为这是一种更合乎逻辑,更清洁的方法.您还将避免在导航抽屉未延伸的状态栏下投射阴影.
如果您希望抽屉延伸到状态栏后面并且具有半透明状态栏大小的叠加层,则可以使用ScrimInsetFrameLayout
抽屉内容(ListView
)作为容器,并使用设置状态栏背景app:insetForeground="#4000"
.当然,你可以改变#4000
你想要的任何东西.别忘了fitsSystemWindows="true"
留在这里!
或者,如果您不想叠加内容并仅显示纯色,则可以将背景设置为您LinearLayout
想要的任何内容.不要忘记分别设置内容的背景!
编辑:您不再需要处理任何此类问题.请参阅设计支持库,以便更轻松地实现导航抽屉/视图实施.
为什么不使用类似的东西呢?
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_ android:layout_ > <FrameLayout android:id="@+id/content_frame" android:layout_ android:layout_/> <ListView android:id="@+id/left_drawer" android:layout_ android:layout_ android:layout_gravity="start" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:divider android:background="#80111111"/> </android.support.v4.widget.DrawerLayout>
上面的代码使用了alpha资源,android:background
以便能够在操作栏中显示透明度.
正如其他答案所示,还有其他方法可以通过代码完成此操作.我上面的回答,在xml布局文件中是必要的,在我看来很容易编辑.
这里提到的所有答案都太陈旧了.与最新Navigationview一起使用的最佳和最短的解决方案是
@Override public void onDrawerSlide(View drawerView, float slideOffset) { super.onDrawerSlide(drawerView, slideOffset); try { //int currentapiVersion = android.os.Build.VERSION.SDK_INT; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){ // Do something for lollipop and above versions Window window = getWindow(); // clear FLAG_TRANSLUCENT_STATUS flag: window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); // finally change the color to any color with transparency window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDarktrans));} } catch (Exception e) { Crashlytics.logException(e); } }
打开抽屉时,这会将状态栏颜色更改为透明
现在当你关闭抽屉时,你需要再次将状态栏颜色更改为暗.所以你可以这样做.
@Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); try { if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { // Do something for lollipop and above versions Window window = getWindow(); // clear FLAG_TRANSLUCENT_STATUS flag: window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); // finally change the color again to dark window.setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark)); } } catch (Exception e) { Crashlytics.logException(e); } }
然后在主布局中添加一行即ie
android:fitsSystemWindows="true"
你的抽屉布局看起来像
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:fitsSystemWindows="true" android:layout_ android:layout_>
你的导航视图看起来像
<android.support.design.widget.NavigationView android:id="@+id/navigation_view" android:layout_ android:layout_ android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/navigation_header" app:menu="@menu/drawer" />
我测试了它并且它完全正常工作.希望它可以帮助某人.这可能不是最好的方法,但它工作顺利,易于实现.标记它是否有帮助.快乐编码:)
如果您希望导航面板位于状态栏上方并且状态栏上方是半透明的.Google I/O Android App Source提供了一个很好的解决方案(Play商店中的APK未更新为持续版本)
首先,您需要一个ScrimInsetFrameLayout
/* * 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); } }
然后,在您的XML布局中更改此部分
<LinearLayout android:id="@+id/linearLayout" android:layout_ android:layout_ android:layout_gravity="left|start" android:fitsSystemWindows="true" android:background="#ffffff"> <ListView android:id="@+id/left_drawer" android:layout_ android:layout_ android:choiceMode="singleChoice"></ListView> </LinearLayout>
像这样将LinearLayout更改为ScrimInsetFrameLayout
<com.boardy.util.ScrimInsetFrameLayout xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/linearLayout" android:layout_ android:layout_ android:layout_gravity="left|start" android:fitsSystemWindows="true" app:insetForeground="#4000"> <ListView android:id="@+id/left_drawer" android:layout_ android:layout_ android:choiceMode="singleChoice"></ListView> </com.boardy.util.ScrimInsetFrameLayout>