视图的垂直排序在 CollapsingToolbarLayout 中如何工作?
How does the vertical ordering of views work in a CollapsingToolbarLayout?
我无法理解 CoordinatorLayout。我想在 imageView 下方放置一个带有 textView 的工具栏。我希望这两个视图都是 CollapsingToolbarLayout 的子视图。我将工具栏放在 imageView 下方,但工具栏实际上出现在 imageView 的顶部。我已经尝试 运行 带有和不带有 layout_anchor 属性的代码,无论哪种方式,我都得到了相同的结果。通过使工具栏成为 CoordinatorLayout 的直接子级,我已经将工具栏显示在 imageView 下方,但这不是最佳选择,因为工具栏可能会与我拥有的 NestedScrollView 重叠。任何帮助我理解这一点的帮助将不胜感激!
fragment_article_detail.xml
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.FloatingActionButton
android:id="@+id/share_fab"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_marginRight="@dimen/fab_margin"
android:background="@drawable/add_fab_background"
android:backgroundTint="@color/theme_accent"
android:backgroundTintMode="src_over"
android:contentDescription="@string/action_share"
android:elevation="@dimen/fab_elevation"
android:src="@drawable/ic_share"
android:stateListAnimator="@anim/fab_state_list_anim"
app:layout_anchor="@id/title.toolbar"
app:layout_anchorGravity="bottom|right|end" />
<android.support.design.widget.AppBarLayout
android:id="@+id/main.appbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:elevation="4dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/main_collapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="@+id/title.toolbar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="24dp"
android:background="@color/brand_primary"
android:elevation="4dp"
app:layout_anchor="@id/main.appbar"
app:layout_anchorGravity="bottom|right"
app:layout_collapseMode="pin"
app:theme="@style/ThemeOverlay.AppCompat.Light">
<LinearLayout
android:id="@+id/book_title_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="8dp"
android:minHeight="?android:attr/actionBarSize"
android:orientation="vertical">
<TextView
android:id="@+id/book.title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="75dp"
android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="@+id/book_body_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:transitionGroup="true">
<TextView
android:id="@+id/article_body"
style="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="72dp"
android:layout_marginRight="16dp"
android:layout_marginTop="40dp"
android:fontFamily="sans-serif"
android:lineSpacingMultiplier="@fraction/detail_body_line_spacing_multiplier"
android:padding="16dp"
android:textSize="20sp" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
注: CollapsingToolbarLayout
扩展 Framelayout
您可以在折叠布局中将重力直接设置到工具栏,以将工具栏放置在底部。
试试这个
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="md.com.collapsinglayoutx.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/main.appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/main_collapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:srcCompat="@drawable/canoe_water_nature"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_gravity="bottom"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:layout_collapseMode="pin">
<LinearLayout
android:id="@+id/book_title_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/book.title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Title"
android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
android:textColor="@color/colorAccent" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include layout="@layout/content_main" />
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
输出
我无法理解 CoordinatorLayout。我想在 imageView 下方放置一个带有 textView 的工具栏。我希望这两个视图都是 CollapsingToolbarLayout 的子视图。我将工具栏放在 imageView 下方,但工具栏实际上出现在 imageView 的顶部。我已经尝试 运行 带有和不带有 layout_anchor 属性的代码,无论哪种方式,我都得到了相同的结果。通过使工具栏成为 CoordinatorLayout 的直接子级,我已经将工具栏显示在 imageView 下方,但这不是最佳选择,因为工具栏可能会与我拥有的 NestedScrollView 重叠。任何帮助我理解这一点的帮助将不胜感激!
fragment_article_detail.xml
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.FloatingActionButton
android:id="@+id/share_fab"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_marginRight="@dimen/fab_margin"
android:background="@drawable/add_fab_background"
android:backgroundTint="@color/theme_accent"
android:backgroundTintMode="src_over"
android:contentDescription="@string/action_share"
android:elevation="@dimen/fab_elevation"
android:src="@drawable/ic_share"
android:stateListAnimator="@anim/fab_state_list_anim"
app:layout_anchor="@id/title.toolbar"
app:layout_anchorGravity="bottom|right|end" />
<android.support.design.widget.AppBarLayout
android:id="@+id/main.appbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:elevation="4dp">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/main_collapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="@+id/title.toolbar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="24dp"
android:background="@color/brand_primary"
android:elevation="4dp"
app:layout_anchor="@id/main.appbar"
app:layout_anchorGravity="bottom|right"
app:layout_collapseMode="pin"
app:theme="@style/ThemeOverlay.AppCompat.Light">
<LinearLayout
android:id="@+id/book_title_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="8dp"
android:minHeight="?android:attr/actionBarSize"
android:orientation="vertical">
<TextView
android:id="@+id/book.title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="75dp"
android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="@+id/book_body_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:transitionGroup="true">
<TextView
android:id="@+id/article_body"
style="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="72dp"
android:layout_marginRight="16dp"
android:layout_marginTop="40dp"
android:fontFamily="sans-serif"
android:lineSpacingMultiplier="@fraction/detail_body_line_spacing_multiplier"
android:padding="16dp"
android:textSize="20sp" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
注: CollapsingToolbarLayout
扩展 Framelayout
您可以在折叠布局中将重力直接设置到工具栏,以将工具栏放置在底部。
试试这个
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="md.com.collapsinglayoutx.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/main.appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/main_collapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/photo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:srcCompat="@drawable/canoe_water_nature"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:layout_gravity="bottom"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:layout_collapseMode="pin">
<LinearLayout
android:id="@+id/book_title_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/book.title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Title"
android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
android:textColor="@color/colorAccent" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include layout="@layout/content_main" />
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
输出