工具栏位于 Appbar 之上 - CoordinatorLayout

Toolbar going on top of Appbar - CoordinatorLayout

我一直在与 AppBarLayout/Toolbar/CoordinatorView 作斗争。希望 Toolbar 在滚动时向上滑动, 和 return 在向下滚动时立即滑动(像大多数阅读应用程序一样,g+也是一个例子)。然而,这就是我得到的:

正常:

一个小卷轴:

满卷:

这是我的代码:

<android.support.design.widget.CoordinatorLayout
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:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="main.MainActivity">

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

        <android.support.v7.widget.Toolbar
        android:id="@+id/id_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlwaysCollapsed"
        android:title="Hello World"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

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


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

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

main__activitycontent:

<android.support.v4.widget.NestedScrollView
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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="main.MainActivity"
tools:showIn="@layout/main__activity">

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/text_margin"
    android:text="@string/large_text"/>

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

我已经阅读了很多博客文章,但他们的 none 配置似乎对我有用。向下滚动时隐藏滚动 up/show 工作正常,只有 Toolbar 在应用栏上移动。检查 Android 视图层次结构,我看到 Toolbar 在深蓝色应用栏上方,并且它们一起向上移动。

编辑 1

CoordinatorLayout 中删除 fitsSytemWindows=true 会导致预期的行为,但应用栏变为白色,无论 CoordinatorLayout 的 bg 颜色如何:

我的猜测是行为实际上没有改变,发生的是它不再覆盖应用栏,所以工具栏不会覆盖它。

编辑 2

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar"/>

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"/>

</resources>

编辑 3

package main;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.ee.oef.refactor.R;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main__activity);
    Toolbar toolbar = (Toolbar) findViewById(R.id.main__toolbar);
    setSupportActionBar(toolbar);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main__menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

问题出在:

app:layout_scrollFlags="scroll|enterAlwaysCollapsed"

您可以将此添加到您的 CollapsingToolbarLayoutenterAlwaysCollapsed CollapsingToolbarLayout 而非 Toolbar 的好习惯].

更新: 我在布局和 ID 中发现了一些问题。

例如,不确定你是否看到了,但我会解释 that.The 首先,Toolbar 的 ID:id_toolbaronCreate 是:

Toolbar toolbar = (Toolbar) findViewById(R.id.main__toolbar);

所以,只需将其更改为:

<android.support.v7.widget.Toolbar
            android:id="@+id/main__toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

P.s: 我刚刚删除了标题和 Scrollflag 标题来自 Manifest -> android:label你需要改变它。(如果你想改变它,以编程方式进行。)

and return immediately on scroll down (like most reading apps, g+ is an example also).

只需将此添加到您的 Toolbar:

app:layout_scrollFlags="scroll|enterAlways"

最后,在这里你可以看到我对这个问题的回答:https://whosebug.com/a/35241363/4409113

我们有:

更新:我没用过:

android:fitsSystemWindows="true"

也在CoordinatorLayout。正如您在此处看到的示例:

https://github.com/chrisbanes/cheesesquare/blob/master/app/src/main/res/layout/include_list_viewpager.xml

更新: 解决工具栏白色问题,设置values-v21/styles.xml:

<item name="android:statusBarColor">@color/colorPrimaryDark</item>

这给出了适当的行为和外观。