如何让水平进度条停留在工具栏顶部

How to let horizontal progress bar stay at the top of toolbar

操作栏

以前在使用Action Bar的时候,很简单的就是在action bar的顶部有横向的进度条。通过代码

@Override
public void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_PROGRESS);

我们可以


工具栏

现在,我希望通过使用 AppCompat Toolbar 实现相同的外观。没有添加水平进度条,我的工具栏是这样的

通过以下布局添加水平进度条后。

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" >

    <!-- android:elevation="4dp" is used due to http://www.google.com/design/spec/what-is-material/elevation-shadows.html#elevation-shadows-elevation-android- -->

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="2dp"
        android:id="@+id/progress"
        android:layout_gravity="top"
        android:gravity="top"
        android:max="100" android:progress="45"/>

</android.support.v7.widget.Toolbar>

然而,这就是我得到的

  1. 水平进度条不在工具栏的顶部,虽然我通过layout_gravitygravity做了具体的top
  2. 工具栏标题文字不见了。

我在想,有什么我遗漏的吗?

改为使用垂直 LinearLayout 作为根:

<LinearLayout
    android:orientation="vertical"
    android:background="?attr/colorPrimary">

    <ProgressBar />

    <android.support.v7.widget.Toolbar />

</LinearLayout>

或者如@CheokYanCheng 在评论中所说,您可能需要使用 FrameLayout 来避免推送效果。

<FrameLayout
    android:gravity="top"
    android:background="?attr/colorPrimary">

    <android.support.v7.widget.Toolbar />

    <ProgressBar />

</FrameLayout>