折叠工具栏 - 状态栏下的工具栏

时间:2016-09-14 14:05:39

标签: android android-coordinatorlayout android-collapsingtoolbarlayout

我最近碰到了协调员布局的问题。当我尝试创建简单的折叠工具栏布局时,如this示例中所示,工具栏apears位于状态栏下,如下面的屏幕截图所示(在preLolipop设备上,一切正常,因为应用程序不在状态栏下绘制)。

toolbar under statusbar (see back arrow)

我的活动布局的代码段:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/imageCalculationDetail"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:background="@drawable/ic_dummy_calculation"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/transparent"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

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

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

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

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

My Styles(仅限v21),其中BaseAppTheme父级是Theme.AppCompat.Light.NoActionBar:

<style name="AppTheme" parent="BaseAppTheme">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="android:colorAccent">@color/colorPrimary</item>
        <item name="android:colorButtonNormal">@color/button_state_list</item>
        <item name="android:statusBarColor">@color/transparent</item>
    </style>

7 个答案:

答案 0 :(得分:5)

我明白了,问题是我有

<item name="android:fitsSystemWindows">true</item>

在BaseAppTheme的toolbarStyle中(对于其他行为,一切正常,对于折叠工具栏而不是半透明状态栏)。设置后

android:fitsSystemWindows="false"

到我的工具栏,一切正常。

答案 1 :(得分:0)

<item name="android:windowDrawsSystemBarBackgrounds">true</item>

通过将此放入您的样式中,您告诉Android您的应用负责绘制系统栏后面的内容。这就是为什么如果设置了这个标志,另一个答案中的“脏黑客”在技术上是正确的。

除非您有明确的原因要求该标志为真(我怀疑没有),否则请将其设置为false,以便操作系统正确地为您的内容抵消。您的状态栏应该仍然是半透明的。

答案 2 :(得分:0)

尝试从ImageView中删除android:fitsSystemWindows="true" ..

<ImageView
            android:id="@+id/imageCalculationDetail"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/ic_dummy_calculation"
            android:scaleType="centerCrop"
            app:layout_collapseMode="parallax"/>

答案 3 :(得分:0)

对我来说

添加属性fitSystemWindows =&#34; false&#34; XML工具栏工作。

<android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:fitsSystemWindows="false"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:layout_collapseMode="pin"/>

答案 4 :(得分:0)

通过编程,您可以通过Android的Window类方法清除并添加相关标志来更改此行为。在有关的“活动”中编写一个自定义函数,并向其传递您选择的颜色资源ID:

科特林

fun setStatusBar(color: Int) {
    val window = this.window
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
    window.statusBarColor = ContextCompat.getColor(this, color)
}

Java

public void setStatusBar(int color) {
    Window window = this.getWindow();
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(ContextCompat.getColor(this, color));
}

然后,在onCreate行之后,从您的Activity覆盖的setContentView(R.layout.your_layout)方法中调用该函数。例如:

科特林

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.your_layout)
    setStatusBar(R.color.colorPrimaryDark)
    ...
}

Java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    setStatusBar(R.color.colorPrimaryDark);
    ...
}

答案 5 :(得分:-1)

应用断点以在棒棒糖设备或更高版本的工具栏中包含marginTop。

值/ dimens.xml

<dimen name="toolbar_margin_top">0dp</dimen>

值-V21 / dimens.xml

<dimen name="toolbar_margin_top">24dp</dimen>

然后在你的布局中:

<android.support.v7.widget.Toolbar
  .....
  android:layout_marginTop="@dimen/toolbar_margin_top"

答案 6 :(得分:-1)

注意对我有用。

对我来说解决方案很简单:

Top CoordinatorLayout 视图

中添加android:fitsSystemWindows="false"