【问题标题】:Progressbar below Toolbar in CoordinatorLayoutCoordinatorLayout 中工具栏下方的进度条
【发布时间】:2016-07-29 14:19:55
【问题描述】:
我有一个这样的布局 XMl:
<CoordinatorLayout>
<AppBarLayout>
<CollapsingToolbarLayout>
<RelativeLayout/>
<Toolbar/>
</CollapsingToolbarLayout>
</AppBarLayout>
<RecyclerView/>
</CoordinatorLayout>
我想在工具栏下方和回收站视图上方添加一个水平进度条,以指示回收站视图项的数据加载。
使用相对布局,我可以简单地为操作栏下方的进度条提供一个 android:layout_below 属性。但是作为 Coordinator Layout 和 Toolbar 的新手,我无法获得结果。
【问题讨论】:
标签:
android
android-toolbar
android-coordinatorlayout
【解决方案1】:
将 ProgressBar 作为 CoordinatorLayout 的直接子级。
<CoordinatorLayout>
<AppBarLayout>
<CollapsingToolbarLayout>
<ImageView/>
<Toolbar/>
</CollapsingToolbarLayout>
</AppBarLayout>
<NestedScrollView>
</NestedScrollView>
<ProgressBar/>
<BottomAppBar/>
<FloatingActionButton/>
</CoordinatorLayout>
对于 ProgressBar,使用 layout_anchor 并将其锚定到 AppBarLayout(等于 AppBarLayout id)。将 layout_anchorGravity 设置为底部。
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
android:visibility="@{viewModel.loading ? View.VISIBLE : View.GONE}"
app:layout_anchor="@id/appBarLayout"
app:layout_anchorGravity="bottom" />
我附上代码和预览截图。
【解决方案2】:
如果你想用Toolbar 隐藏ProgressBar 将Toolbar 包裹在LinearLayout 中,并将ProgressBar 作为LinearLayout 的第一个孩子。分配
orientation="vertical"
如果您希望ProgressBar 始终可见,则将RecyclerView 包裹在LinearLayout 中并将ProgressBar 作为第一个孩子。设置方向。记得加
app:layout_behavior="@string/appbar_scrolling_view_behavior"
作为LinearLayout的参数(它必须将滚动传递给孩子)
【解决方案3】:
<CoordinatorLayout>
<AppBarLayout>
<CollapsingToolbarLayout>
<RelativeLayout/>
<Toolbar/>
</CollapsingToolbarLayout>
</AppBarLayout>
<LinearLayout>
<ProgressBar>
<RecyclerView/>
</LinearLayout>
</CoordinatorLayout>
以上内容应该可以满足您的需求。确保线性布局上的方向是垂直的。唯一需要注意的是,如果您的回收器视图很大,您可能需要将线性布局包装在嵌套滚动视图中,假设您希望进度条也滚动。