CollapsingToolbarLayout 海拔不工作和锚定 FloatingActionButton 不返回
CollapsingToolbarLayout elevation not working and anchored FloatingActionButton not returning
我一直在尝试实现布局 similar to this from the material design guidelines, using the new Android Design Support Library。 CollapsingToolbarLayout
似乎是要走的路,但它忽略了任何增加海拔的尝试。此外,如果我将 AppBarLayout
的高度设置为低于 144dp 的任何值,FloatingActionButton
将弹出,但 return 不会弹出。这是我的 xml:
<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/appbar"
android:layout_width="match_parent"
android:layout_height="144dp"
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"
app:expandedTitleMarginEnd="72dp"
app:expandedTitleMarginStart="72dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:paddingLeft="72dp"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_person_add_white_24dp"
app:borderWidth="0dp"
app:fabSize="mini"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|left|start"/>
深入了解源代码后,我发现 FloatingActionButton
,当附加到 AppBarLayout
时,设置为仅在可用的 space 为 <=appBarLayout.getMinimumHeightForVisibleOverlappingContent()
时出现,即 2* 最小高度(在本例中为 Toolbar
)加上状态栏。所以在 phone 上的纵向,56*2+26 = 138dp。我通过将我的设置为 138dp 来确认这一点,这有效,而 137dp 则无效。如果你想解决这个问题,你可以覆盖 AppBarLayout
中的 getMinimumHeightForVisibleOverlappingContent()
,或者 FloatingActionButton
中的以下方法:
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
if(dependency instanceof SnackbarLayout) {
this.updateFabTranslationForSnackbar(parent, child, dependency);
} else if(dependency instanceof AppBarLayout) {
AppBarLayout appBarLayout = (AppBarLayout)dependency;
if(this.mTmpRect == null) {
this.mTmpRect = new Rect();
}
Rect rect = this.mTmpRect;
ViewGroupUtils.getDescendantRect(parent, dependency, rect);
if(rect.bottom <= appBarLayout.getMinimumHeightForVisibleOverlappingContent()) {
if(!this.mIsAnimatingOut && child.getVisibility() == 0) {
this.animateOut(child);
}
} else if(child.getVisibility() != 0) {
this.animateIn(child);
}
}
return false;
}
至于 CollapsingToolbarLayout
提升,我还没有找到解决方法。
对于 CollapsingToolbarLayout
海拔,您可以在子类 CollapsingToolbarLayout.OffsetUpdateListener
中搜索,该子类具有方法 onOffsetChanged
来完成这项工作
if(Math.abs(verticalOffset) == scrollRange) {
ViewCompat.setElevation(layout, layout.getTargetElevation());
} else {
ViewCompat.setElevation(layout, 0.0F);
}
关于高度,折叠工具栏布局在未折叠时明确删除高度*。它似乎已被归档为错误:
(正如@Ben 在他的回答和对源代码的引用中所说的那样)
直接link到代码(当前,第1062行):
https://android.googlesource.com/platform/frameworks/support/+/master/design/src/android/support/design/widget/CollapsingToolbarLayout.java#1062
if (Math.abs(verticalOffset) == scrollRange) {
// If we have some pinned children, and we're offset to only show those views,
// we want to be elevate
ViewCompat.setElevation(layout, layout.getTargetElevation());
} else {
// Otherwise, we're inline with the content
ViewCompat.setElevation(layout, 0f);
}
我一直在尝试实现布局 similar to this from the material design guidelines, using the new Android Design Support Library。 CollapsingToolbarLayout
似乎是要走的路,但它忽略了任何增加海拔的尝试。此外,如果我将 AppBarLayout
的高度设置为低于 144dp 的任何值,FloatingActionButton
将弹出,但 return 不会弹出。这是我的 xml:
<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/appbar"
android:layout_width="match_parent"
android:layout_height="144dp"
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"
app:expandedTitleMarginEnd="72dp"
app:expandedTitleMarginStart="72dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:paddingLeft="72dp"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/ic_person_add_white_24dp"
app:borderWidth="0dp"
app:fabSize="mini"
app:layout_anchor="@id/appbar"
app:layout_anchorGravity="bottom|left|start"/>
深入了解源代码后,我发现 FloatingActionButton
,当附加到 AppBarLayout
时,设置为仅在可用的 space 为 <=appBarLayout.getMinimumHeightForVisibleOverlappingContent()
时出现,即 2* 最小高度(在本例中为 Toolbar
)加上状态栏。所以在 phone 上的纵向,56*2+26 = 138dp。我通过将我的设置为 138dp 来确认这一点,这有效,而 137dp 则无效。如果你想解决这个问题,你可以覆盖 AppBarLayout
中的 getMinimumHeightForVisibleOverlappingContent()
,或者 FloatingActionButton
中的以下方法:
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
if(dependency instanceof SnackbarLayout) {
this.updateFabTranslationForSnackbar(parent, child, dependency);
} else if(dependency instanceof AppBarLayout) {
AppBarLayout appBarLayout = (AppBarLayout)dependency;
if(this.mTmpRect == null) {
this.mTmpRect = new Rect();
}
Rect rect = this.mTmpRect;
ViewGroupUtils.getDescendantRect(parent, dependency, rect);
if(rect.bottom <= appBarLayout.getMinimumHeightForVisibleOverlappingContent()) {
if(!this.mIsAnimatingOut && child.getVisibility() == 0) {
this.animateOut(child);
}
} else if(child.getVisibility() != 0) {
this.animateIn(child);
}
}
return false;
}
至于 CollapsingToolbarLayout
提升,我还没有找到解决方法。
对于 CollapsingToolbarLayout
海拔,您可以在子类 CollapsingToolbarLayout.OffsetUpdateListener
中搜索,该子类具有方法 onOffsetChanged
来完成这项工作
if(Math.abs(verticalOffset) == scrollRange) {
ViewCompat.setElevation(layout, layout.getTargetElevation());
} else {
ViewCompat.setElevation(layout, 0.0F);
}
关于高度,折叠工具栏布局在未折叠时明确删除高度*。它似乎已被归档为错误:
(正如@Ben 在他的回答和对源代码的引用中所说的那样)
直接link到代码(当前,第1062行): https://android.googlesource.com/platform/frameworks/support/+/master/design/src/android/support/design/widget/CollapsingToolbarLayout.java#1062
if (Math.abs(verticalOffset) == scrollRange) {
// If we have some pinned children, and we're offset to only show those views,
// we want to be elevate
ViewCompat.setElevation(layout, layout.getTargetElevation());
} else {
// Otherwise, we're inline with the content
ViewCompat.setElevation(layout, 0f);
}