2016-03-29 115 views
1

如果我能够与半透明导航栏是纯色

getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 

getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); 

然后在工具栏是状态栏的后面(状态栏应该是彩色的,但导航栏应该是半透明)的半透明导航栏。但是,如果我转到此活动的布局XML并放入android:fitsSystemWindows="true",工具栏已正确布置,但导航栏则是纯色。

这里的布局:

<?xml version="1.0" encoding="utf-8"?> 
<me.ccrama.redditslide.Views.SidebarLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:background="?attr/activity_background"> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <View 
      android:id="@+id/anchor" 
      android:layout_width="1dp" 
      android:layout_marginTop="4dp" 
      android:layout_marginRight="3dp" 

      android:layout_gravity="top|right" 
      android:layout_height="1dp" /> 

     <me.ccrama.redditslide.Views.ToggleSwipeViewPager 
      android:id="@+id/content_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/header" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:minHeight="56dp" 
       android:layout_height="?android:attr/actionBarSize" 
       android:theme="@style/ActionBarCompat" 
       android:title="@string/app_name" /> 

      <ViewStub 
       android:id="@+id/stub_tabs" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:inflatedId="@+id/sliding_tabs" 
       android:layout="@layout/activity_overview_tabs" /> 

     </android.support.design.widget.AppBarLayout> 


    </FrameLayout> 

    <ListView 
     android:id="@+id/drawerlistview" 
     android:layout_width="300dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="?attr/card_background" 
     android:orientation="vertical" /> 

    <include 
     layout="@layout/subinfo" 
     android:layout_width="300dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="end" /> 

</me.ccrama.redditslide.Views.SidebarLayout> 

同样的行为(彩色导航栏)发生,如果我把android:fitsSystemWindows="true"FrameLayout,而不是内部。

下面的第一张图片是当工具栏布局混乱,第二部手机是导航栏不透明时。下面

enter image description here

enter image description here

+0

getWindow()setStatusBarColor(Color.TRANSPARENT) ; –

+0

但如果我为状态栏着色会怎么样?我已经将它设置为红色。 – Nxt3

+0

你的意思是? –

回答

1

使用主题为您的活动主题:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

并使用getNavigationBarColor(Color.TRANSPARENT);

窗口管理

  /** 
    * Flag indicating that this Window is responsible for drawing the background for the 
    * system bars. If set, the system bars are drawn with a transparent background and the 
    * corresponding areas in this window are filled with the colors specified in 
    * {@link Window#getStatusBarColor()} and {@link Window#getNavigationBarColor()}. 
    */ 
    public static final int FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS = 0x80000000; 


    /** 
    * Window flag: request a translucent status bar with minimal system-provided 
    * background protection. 
    * 
    * <p>This flag can be controlled in your theme through the 
    * {@link android.R.attr#windowTranslucentStatus} attribute; this attribute 
    * is automatically set for you in the standard translucent decor themes 
    * such as 
    * {@link android.R.style#Theme_Holo_NoActionBar_TranslucentDecor}, 
    * {@link android.R.style#Theme_Holo_Light_NoActionBar_TranslucentDecor}, 
    * {@link android.R.style#Theme_DeviceDefault_NoActionBar_TranslucentDecor}, and 
    * {@link android.R.style#Theme_DeviceDefault_Light_NoActionBar_TranslucentDecor}.</p> 
    * 
    * <p>When this flag is enabled for a window, it automatically sets 
    * the system UI visibility flags {@link View#SYSTEM_UI_FLAG_LAYOUT_STABLE} and 
    * {@link View#SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.</p> 
    */ 
    public static final int FLAG_TRANSLUCENT_STATUS = 0x04000000; 

窗口

/** 
* Sets the color of the status bar to {@code color}. 
* 
* For this to take effect, 
* the window must be drawing the system bar backgrounds with 
* {@link android.view.WindowManager.LayoutParams#FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS} and 
* {@link android.view.WindowManager.LayoutParams#FLAG_TRANSLUCENT_STATUS} must not be set. 
* 
* If {@code color} is not opaque, consider setting 
* {@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_STABLE} and 
* {@link android.view.View#SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}. 
* <p> 
* The transitionName for the view background will be "android:status:background". 
* </p> 
*/ 
public abstract void setStatusBarColor(@ColorInt int color); 
+0

对不起 - 什么?你说过“在下面删除”?此外,我不希望状态栏是透明的 - 只有导航栏。 – Nxt3

+0

我的意思是你应该删除我上面列出的代码。请提供你的主题代码。 –

+0

我没有任何地方的任何代码。我发布了所有我需要设置导航栏半透明的相关代码。 – Nxt3

0

doc

如果您要创建一个自定义主题,设置这些主题为 父主题或一个包括windowTranslucentNavigation和 windowTranslucentStatus样式pr在你的主题operties。

所以,你可以使用下面的主题,但对于API 19+:

<style name="Theme.DesignDemo" parent="Base.Theme.DesignDemo"> 
</style> 

<style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar"> 
    <item name="colorPrimary">#673AB7</item> 
    <item name="colorPrimaryDark">#512DA8</item> 
    <item name="colorAccent">#FF4081</item> 
    <item name="android:windowTranslucentNavigation">true</item> 
</style> 

值,21:

<style name="Theme.DesignDemo" parent="Base.Theme.DesignDemo"> 
     <item name="android:windowDrawsSystemBarBackgrounds">true</item> 
     <item name="android:statusBarColor">@android:color/transparent</item> 
    </style> 
+0

我还需要做Java部分来设置半透明吗?我已经尝试了你的建议,但没有奏效。 – Nxt3

+0

我试过它没有国旗的lolipop,它的工作,我不知道你的问题是什么,试试看。 –

+0

我添加了所有样式 –