android - CollapsingToolbarLayout 折叠标题位置不正确

标签 android android-collapsingtoolbarlayout

我有一个直接的 CollapsingToolbarLayout。 到目前为止它工作正常,但如果我折叠工具栏,标题的位置不会垂直居中。

这是我的布局:

<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:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/appbar_header_height_expanded"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:scaleType="centerCrop"
            android:src="@drawable/placekitten_1"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            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:clipToPadding="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Text" />

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

看看我的屏幕截图,这样您就可以了解它在两种状态下的行为。

expanded toolbar

这是标题折叠工具栏的问题。

enter image description here

我的 BaseFragment 中有这个,可以在实际的 Fragment 中设置我的工具栏:

protected void setToolbar(View view, int resource, String title, String subtitle) {
    Toolbar toolbar = view.findViewById(resource);
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(title);
    ((AppCompatActivity) getActivity()).getSupportActionBar().setSubtitle(subtitle);
}

所以我发现,.. 如果我显示一个 Snackbar,那么标题会跳到正确的位置并且展开/折叠工作正常!那么你知道为什么它在显示 Snackbar 后会起作用吗?

View view = findViewById(R.id.content_frame);
    Snackbar mySnackbar = Snackbar.make(view, text, Snackbar.LENGTH_LONG);
    mySnackbar.getView().setBackgroundColor(getResources().getColor(color));
    mySnackbar.show();

content_frame.xml 是主要 Activity 的根布局:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true" />

有什么想法吗?

最佳答案

问题是根 CoordinatorLayout 中的 android:fitsSystemWindows="true"。从 root layout 中删除该属性,标题就会正常运行。

关于android - CollapsingToolbarLayout 折叠标题位置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46909486/

相关文章:

android - Facebook Activity 无法在 facebook-sdk 4.4.0 中正确加载

java - 错误: No resource found that matches the given name (at 'background' with value '@drawable-mdpi/1.jpg' )

android - CollapsingToolBarLayout - 状态栏稀松布颜色不会改变

android - 视差效果和捕捉工具栏,如 Google Play 商店应用

java - 从 GooglePlus 登录获取访问 token

java - android studio 中包含文件的包或文件夹

android - 如何从另一个 Activity 更改布局背景?

android - 后退按钮未显示在 CollapsingToolbarLayout 中

android - 折叠工具栏布局和约束布局

android - 如何删除appbarlayout android中工具栏顶部的额外填充