在工具栏中显示 ProgressBar

Display ProgressBar in Toolbar

我想在 activity 工具栏底部的更新过程中显示进度条。但是,我仍然没有找到合适的解决方案。我应该如何开始?

我已经尝试过使用 FrameLayout 或将 ProgressBar 放入工具栏。不幸的是,它看起来与这些相似:

如何做这样的事情?我的意图是当 ProgressBar 消失时布局不会改变。

尝试按照您的 xml 布局

<?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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <!-- Here your ProgressBar -->

        <ProgressBar
            android:id="@+id/progress"
            android:layout_marginTop="-7dp"
            android:layout_marginBottom="-7dp"
            android:indeterminate="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal" />

    </android.support.design.widget.AppBarLayout>

    <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:fabSize="normal"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

结果会是这样

接受的答案在大多数情况下应该足够了,但如果您不想处理填充值,您可以包装 ProgressIndicatorToolbar,这样 ProgressIndicator 就可以了在 Toolbar.

之上
<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/Theme.MyApp.AppBarOverlay">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/Theme.MyApp.PopupOverlay" />

        <com.google.android.material.progressindicator.LinearProgressIndicator
            android:id="@+id/progress_indicator"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:indeterminate="true"
            app:hideAnimationBehavior="inward"
            app:indicatorColor="@color/colorSecondary"
            app:showAnimationBehavior="outward" />
    </FrameLayout>
</com.google.android.material.appbar.AppBarLayout>

区别在于,如果ProgressIndicator绘制在Toolbar之上,那么AppBarLayout的高度保持不变。

[With FrameLayout]                   [Without FrameLayout]
+-------------------+   ^            +-------------------+   ^
|                   |   |            |                   |   |
|                   |   |            |                   |   |
|                   | AppBarLayout   |                   |   |
+-------------------+   |            |                   | AppBarLayout
| ProgressIndicator |   |            |                   |   |
+-------------------+   V            +-------------------+   |
|                   |                | ProgressIndicator |   |
|                   |                +-------------------+   v
|                   |                |                   |
.                   .                .                   .
.                   .                .                   .