【发布时间】: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