【问题标题】:CollapsingToolbarLayout collapsed title has bad positionCollapsingToolbarLayout 折叠的标题位置不正确
【发布时间】:2017-10-24 11:34:29
【问题描述】:

我有一个直截了当的 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>

看看我的截图,你可以看到它在两种状态下的表现。

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

我的 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 是主要活动的根布局:

<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" />

有什么想法吗?

【问题讨论】:

  • 你能分享你的活动代码和主题吗?你的布局看起来一切正常。
  • 这显然是一个已知问题issuetracker.google.com/issues/37140811。但我永远不会面对它.. 你使用 appcompat 最新版本吗?
  • 相同的布局在一个片段中有效,但在其他片段中无效。这很令人困惑。是的,我使用的是最新的 26.1.0。
  • 您是否尝试过将 android:theme="@style/AppTheme.AppBarOverlay" 用于 AppBarLayout 和 app:popupTheme="@style/AppTheme.PopupOverlay" 用于工具栏?这是现在 AS 活动生成器提供的默认主题。
  • 我改了样式,但是没有效果

标签: android android-collapsingtoolbarlayout


【解决方案1】:

问题是根CoordinatorLayout 中的android:fitsSystemWindows="true"。从 根布局 中删除该属性,并且标题表现良好。

【讨论】:

    【解决方案2】:

    我在此链接末尾尝试了解决方案:http://www.solutionscan.org/43500-android,它对我有用...

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        // ...
        collapsing_toolbar.post { collapsing_toolbar.requestLayout() }
    }
    

    唯一的区别是我使用的是片段和新的导航组件...

    【讨论】:

    • 这个解决方案也帮助了我。花了将近一天时间尝试android:fitsSystemWindows 的不同组合。非常不明显。
    • @despectra 我也不完全明白为什么需要刷新布局:(
    【解决方案3】:

    这是关于

    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);

    它总是得到对应于第一个片段的那个,所以对于其他片段它不起作用。 (只有当我将工具栏 ID 更改为错误的 ID 时,我才能重现该错误,这就是为什么您的片段之一没有问题的原因)

    尝试在不同的片段布局中为每个 Toolbar 组件设置一个特定的 id,或者如果所有片段的 layout.xml 都相同,请想办法确保 setSupportActionBar 与您当前片段的确切工具栏视图。

    【讨论】:

    • 好的,谢谢你的提示,但我认为这没有什么问题,因为我已经尝试为所有不同的片段提供不同的工具栏 ID,但它没有任何改变。对于每个视图,我都有一个不同的 layout.xml,里面有不同的工具栏。但我会再检查一遍
    • 所以多次使用 setSupportActionBar 会出现问题?
    • 不..,看 setSupportActionBar 工作基本正常,我有 1 个活动和大约 10 个片段。所有片段都可以与自己的 layout.xml 一起正常工作,并且每个 layout.xml 都可以与自己的特定工具栏一起正常工作。一个 CollapsingToolbarLayout 也可以正常工作,但其他的不能正常工作,它们有给定的问题。不知道什么时候出现问题。如果我知道那我就不会有这个问题了嘿嘿
    • 我发现它出现时,问题仍然存在,请查看已编辑的问题。
    • 这是一个已知问题:stack question
    【解决方案4】:

    将 android:fitsSystemWindows="true" 行也添加到工具栏。

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

    【讨论】:

      猜你喜欢
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      • 2015-10-18
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多