android – 将导航栏放在状态栏下

前端之家收集整理的这篇文章主要介绍了android – 将导航栏放在状态栏下前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让我的导航栏进入状态栏.我已经广泛阅读了关于ScrimInsetsFrameLayout视图,我尝试实现它,但由于某种原因,它不会被忽略.

这是我使用/写的代码.

XML DrawerLayout:

  1. <android.support.v4.widget.DrawerLayout
  2. android:id="@+id/drawer_layout"
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6.  
  7. <FrameLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:orientation="vertical">
  11.  
  12. <FrameLayout
  13. android:id="@+id/content_frame"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent" />
  16.  
  17. <include layout="@layout/toolbar" />
  18.  
  19. </FrameLayout>
  20.  
  21. <com.andrewq.planets.util.ScrimInsetsFrameLayout
  22. xmlns:app="http://schemas.android.com/apk/res-auto"
  23. android:id="@+id/linearLayout"
  24. android:layout_width="304dp"
  25. android:layout_height="match_parent"
  26. android:layout_gravity="start"
  27. android:fitsSystemWindows="true"
  28. app:insetForeground="#4000">
  29.  
  30. <ListView
  31. android:id="@+id/left_drawer"
  32. android:layout_width="match_parent"
  33. android:layout_height="match_parent"
  34. android:choiceMode="singleChoice" />
  35. </com.andrewq.planets.util.ScrimInsetsFrameLayout>
  36.  
  37.  
  38. </android.support.v4.widget.DrawerLayout>

ScrimInsetsFrameLayout.java:

  1. package com.andrewq.planets.util;
  2.  
  3. /*
  4. * Copyright 2014 Google Inc.
  5. *
  6. * Licensed under the Apache License,Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,software
  13. * distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17.  
  18. import android.content.Context;
  19. import android.content.res.TypedArray;
  20. import android.graphics.Canvas;
  21. import android.graphics.Rect;
  22. import android.graphics.drawable.Drawable;
  23. import android.support.v4.view.ViewCompat;
  24. import android.util.AttributeSet;
  25. import android.widget.FrameLayout;
  26.  
  27. import com.andrewq.planets.R;
  28.  
  29. /**
  30. * A layout that draws something in the insets passed to {@link #fitSystemWindows(Rect)},i.e. the area above UI chrome
  31. * (status and navigation bars,overlay action bars).
  32. */
  33. public class ScrimInsetsFrameLayout extends FrameLayout {
  34. private Drawable mInsetForeground;
  35.  
  36. private Rect mInsets;
  37. private Rect mTempRect = new Rect();
  38. private OnInsetsCallback mOnInsetsCallback;
  39.  
  40. public ScrimInsetsFrameLayout(Context context) {
  41. super(context);
  42. init(context,null,0);
  43. }
  44.  
  45. public ScrimInsetsFrameLayout(Context context,AttributeSet attrs) {
  46. super(context,attrs);
  47. init(context,attrs,AttributeSet attrs,int defStyle) {
  48. super(context,defStyle);
  49. init(context,defStyle);
  50. }
  51.  
  52. private void init(Context context,int defStyle) {
  53. final TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ScrimInsetsView,defStyle,0);
  54. if (a == null) {
  55. return;
  56. }
  57. mInsetForeground = a.getDrawable(R.styleable.ScrimInsetsView_insetForeground);
  58. a.recycle();
  59.  
  60. setWillNotDraw(true);
  61. }
  62.  
  63. @Override
  64. protected boolean fitSystemWindows(Rect insets) {
  65. mInsets = new Rect(insets);
  66. setWillNotDraw(mInsetForeground == null);
  67. ViewCompat.postInvalidateOnAnimation(this);
  68. if (mOnInsetsCallback != null) {
  69. mOnInsetsCallback.onInsetsChanged(insets);
  70. }
  71. return true; // consume insets
  72. }
  73.  
  74. @Override
  75. public void draw(Canvas canvas) {
  76. super.draw(canvas);
  77.  
  78. int width = getWidth();
  79. int height = getHeight();
  80. if (mInsets != null && mInsetForeground != null) {
  81. int sc = canvas.save();
  82. canvas.translate(getScrollX(),getScrollY());
  83.  
  84. // Top
  85. mTempRect.set(0,width,mInsets.top);
  86. mInsetForeground.setBounds(mTempRect);
  87. mInsetForeground.draw(canvas);
  88.  
  89. // Bottom
  90. mTempRect.set(0,height - mInsets.bottom,height);
  91. mInsetForeground.setBounds(mTempRect);
  92. mInsetForeground.draw(canvas);
  93.  
  94. // Left
  95. mTempRect.set(0,mInsets.top,mInsets.left,height - mInsets.bottom);
  96. mInsetForeground.setBounds(mTempRect);
  97. mInsetForeground.draw(canvas);
  98.  
  99. // Right
  100. mTempRect.set(width - mInsets.right,height - mInsets.bottom);
  101. mInsetForeground.setBounds(mTempRect);
  102. mInsetForeground.draw(canvas);
  103.  
  104. canvas.restoreToCount(sc);
  105. }
  106. }
  107.  
  108. @Override
  109. protected void onAttachedToWindow() {
  110. super.onAttachedToWindow();
  111. if (mInsetForeground != null) {
  112. mInsetForeground.setCallback(this);
  113. }
  114. }
  115.  
  116. @Override
  117. protected void onDetachedFromWindow() {
  118. super.onDetachedFromWindow();
  119. if (mInsetForeground != null) {
  120. mInsetForeground.setCallback(null);
  121. }
  122. }
  123.  
  124. /**
  125. * Allows the calling container to specify a callback for custom processing when insets change (i.e. when
  126. * {@link #fitSystemWindows(Rect)} is called. This is useful for setting padding on UI elements based on
  127. * UI chrome insets (e.g. a Google Map or a ListView). When using with ListView or GridView,remember to set
  128. * clipToPadding to false.
  129. */
  130. public void setOnInsetsCallback(OnInsetsCallback onInsetsCallback) {
  131. mOnInsetsCallback = onInsetsCallback;
  132. }
  133.  
  134. public static interface OnInsetsCallback {
  135. public void onInsetsChanged(Rect insets);
  136. }
  137. }

最后,这里是我的styles.xml for values-v21:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.  
  4. <style name="AppThemeNavDrawer" parent="Theme.AppCompat.NoActionBar">
  5. <item name="colorAccent">#F8F8F8</item>
  6. <item name="android:windowTranslucentStatus">true</item>
  7.  
  8. <item name="windowActionBar">false</item>
  9. <item name="windowActionModeOverlay">true</item>
  10. <item name="android:windowDrawsSystemBarBackgrounds">true</item>
  11. <item name="android:statusBarColor">@android:color/transparent</item>
  12. </style>
  13. </resources>

我看过2014年的I / O应用源代码以及this的问题,我不知道有什么不同.

这是一个屏幕截图,我到目前为止减去状态栏下的抽屉:

我有一切工作完美,这是我需要做的最后一件事.帮助非常感谢!

编辑:

为了弄清楚,我想像在大多数Google Apps和Google Now中一样,将状态栏中的图像着色.

解决方法

有不同的方法来达到预期的效果.您可以通过样式或代码启用半透明.

我创建了一个MaterialDrawer(遵循Android材料设计指南),它实现了所有这一切,并为您处理一切.在这里阅读更多:https://github.com/mikepenz/MaterialDrawer/

如果你想自己创建它,你总是要决定哪个是你最需要支持的API,和/或者你必须分开你的样式.

所以要启用半透明状态栏,您必须至少在API v19上,或者为v19创建一个分离样式-v19
这样会看起来像这样

  1. <style name="YourTheme.TranslucentStatus" parent="Theme.AppCompat.Light.NoActionBar">
  2. <item name="android:windowTranslucentStatus">true</item>
  3. </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.

编辑:

关于如何以编程方式处理这个更复杂的示例:

  1. if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
  2. //enable translucent statusbar via flags
  3. setTranslucentStatusFlag(true);
  4. }
  5. if (Build.VERSION.SDK_INT >= 19) {
  6. mActivity.getWindow().getDecorView().setsystemUIVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
  7. }
  8. if (Build.VERSION.SDK_INT >= 21) {
  9. //we don't need the translucent flag this is handled by the theme
  10. setTranslucentStatusFlag(false);
  11. //set the statusbarcolor transparent to remove the black shadow
  12. mActivity.getWindow().setStatusBarColor(Color.TRANSPARENT);
  13. }
  14.  
  15. //add a padding to the content of the drawer (25dp on devices starting with api v19)
  16. mDrawerContentRoot.setPadding(0,mActivity.getResources().getDimensionPixelSize(R.dimen.tool_bar_top_padding),0);
  17.  
  18. // define the statusBarColor
  19. mDrawerContentRoot.setInsetForeground(mStatusBarColor);
  20.  
  21.  
  22. private void setTranslucentStatusFlag(boolean on) {
  23. if (Build.VERSION.SDK_INT >= 19) {
  24. Window win = mActivity.getWindow();
  25. WindowManager.LayoutParams winParams = win.getAttributes();
  26. final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
  27. if (on) {
  28. winParams.flags |= bits;
  29. } else {
  30. winParams.flags &= ~bits;
  31. }
  32. win.setAttributes(winParams);
  33. }
  34. }

EDIT2:

解决此问题的完整解决方案是清理项目中的所有布局.一些布局和风格的组合引起了麻烦.

完整的更改可以在这个拉动请求中找到:
https://github.com/Andrew-Quebe/Planets-Gradle/commit/83e28c09253af6e807b6f4e94baca8fbca3fc7c8

猜你在找的Android相关文章