2016-01-25 151 views
1

这是我遇到状态栏重叠工具栏上的预棒棒糖,但不是棒棒糖/棉花糖

在棒棒糖前期设备我有以下问题

enter image description here

在棒棒糖和问题棉花糖设备一切都显得精细

enter image description here

我试图创建打开导航抽屉时显示半透明状态栏效果。 在棉花糖和棒棒糖设备上,这工作正常。

这里是我的代码

activity_base.xml

<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/drawerLayout" 
    android:fitsSystemWindows="true" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/colorPrimary" 
      android:minHeight="?attr/actionBarSize" 
      android:theme="@style/ToolbarStyle" 
      app:theme="@style/ToolbarStyle" /> 

     <FrameLayout 
      android:id="@+id/activity_content" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

    <include layout="@layout/view_navigation_drawer_layout" /> 

</android.support.v4.widget.DrawerLayout> 

view_navigation_drawer_layout.xml

<android.support.design.widget.NavigationView 
    android:id="@+id/navigation" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <include layout="@layout/nav_header" /> 

     <ListView 
      android:id="@+id/lst_menu_items" 
      android:layout_width="match_parent" 
      android:layout_height="0dp" 
      android:layout_weight="1" /> 
    </LinearLayout> 
</android.support.design.widget.NavigationView> 

请注意,我做,因为这个问题的答案NavigationView and custom Layout以上

注意两个布局有android:fitsSystemWindows="true"

我想,也许它是因为我有布局,所以我尝试复制整个布局到活动布局。没有帮助

我也有我的价值观-V19目录这个款式的xml文件(KitKat及以上)

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="AppTheme" parent="MaterialDesign"> 
     <item name="android:windowTranslucentStatus">true</item> 
    </style> 
</resources> 

回答

0

我做了一个解决这个问题。然而,我的解决方案需要劫持的布局在这个博客帖子http://mateoj.com/2015/06/21/adding-toolbar-and-navigation-drawer-all-activities-android/

这里说一个BaseActivity是我的代码

public class BaseActivity extends AppCompatActivity { 


    @Override 
    public void setContentView(int layoutResID) { 
     ViewGroup baseView = (ViewGroup) getLayoutInflater().inflate(R.layout.activity_base_with_drawer); 
     FrameLayout activityContainer = (FrameLayout) baseView.findViewById(R.id.activity_content); 
     getLayoutInflater().inflate(layoutResID, activityContainer, true); 
     super.setContentView(layoutResID); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     if (useToolbar()) { //Does the activity have a toolbar? 
      toolbar.setFitsSystemWindows(true); //toolbar will stretch underneath the status bar and because it naturally has a semi translucent background, it will assume a darker color of the toolbar which is usually the color primary. 
      setSupportActionBar(toolbar); //Set the toolbar 
      ActionBar actionBar = getSupportActionBar(); 
      if (actionBar != null) { 
       actionBar.setDisplayHomeAsUpEnabled(true); 
      } 
     } else { 
      toolbar.setVisibility(View.GONE); 
      activityContainer.setFitsSystemWindows(true); //No toolbar so we set the container to have the fitsSystemWindow attribute to ensure contents do not overlay the system window such as status bar and navigation bar. 
      TypedValue outValue = new TypedValue(); 
      boolean doesThemeHaveTranslucentWindow = getTheme().resolveAttribute(android.R.attr.windowIsTranslucent, outValue, true); //Check if the current activity theme has translucent window, this includes SearchActivity 
      if (!doesThemeHaveTranslucentWindow) { //If it does not then set the container background to colour primary to have the same effect as the toolbar being there and avoid a white status bar. We do not want to do this for translucent windows otherwise they would not be see through 
       activityContainer.setBackgroundColor(ContextCompat.getColor(this, R.color.colorPrimary)); 
      } 
     } 
    } 

这是布局

<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/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@color/colorPrimary" 
      android:minHeight="?attr/actionBarSize" 
      android:theme="@style/ToolbarStyle" 
      app:theme="@style/ToolbarStyle" /> 

     <FrameLayout 
      android:id="@+id/activity_content" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 
    </LinearLayout> 

    <include layout="@layout/view_navigation_drawer_layout" /> 

</android.support.v4.widget.DrawerLayout> 

FitsSystemWindows不再设在任何XML布局中。这是在BaseActivity类的setContentViewMethod中完成的。 如果该活动没有工具栏,则活动容器将fitsSystemWindow属性设置为true,否则应用该工具栏。

如果活动没有工具栏,则其容器布局的背景颜色设置为颜色主要。

为什么我这样做是因为当您在您的工具栏上设置fitsSystemWindows时,它会伸展到状态栏下方,并且由于状态栏自然具有半透明背景,因此它会呈现工具栏的较暗颜色,通常是颜色主要所以我们这样做的活动容器具有相同的效果。

现在有一个用例,我在我的应用程序中,我有一个透明窗口背景的活动,我检查。如果不是这样,则活动容器具有此颜色集。

不知道它是否是最好的解决方案,但它迄今在棉花糖,前棒棒糖和pre-KitKat上的运行状况也很好:P。希望它能帮助别人。