工具栏配有水平进度条

6

随着Android Lollipop和AppCompat-v7中的新Toolbar API,它们正在删除许多自动功能,以使Toolbar/ActionBar更加强大。其中之一是进度条。由于Toolbar只是一个ViewGroup,我认为添加ProgressBar应该很简单。但是,我似乎无法使其正常工作。

我已经采取了以下措施(使用SmoothProgressBar库):

    // I instantiate the toolbar and set it as the actionbar
    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    // I create a ProgressBar and set the drawable to the SmoothProgressBar drawable
    ProgressBar progressBar = new ProgressBar(this);
    progressBar.setIndeterminateDrawable(new SmoothProgressDrawable.Builder(this).color(Color.BLUE).interpolator
            (new DecelerateInterpolator()).sectionsCount(4).separatorLength(8).speed(2f).mirrorMode(true).build());
    // I add the progressbar to the view with what I thought were the proper LayoutParams.
    Toolbar.LayoutParams params = new Toolbar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 20);
    params.gravity = Gravity.BOTTOM;
    mToolbar.addView(progressBar, params);
    progressBar.setIndeterminate(true);

我原以为这会有效,因为我只是在ViewGroup底部添加了一个ProgressBar。然而,它根本没有显示出来,并且还删除了标题。如下图所示,在之前和之后都可以看到这种情况。有人知道如何解决吗?我的目标是在ActionBar下方放置一个ProgressBar。
3个回答

5

最简单的方法是直接在XML布局文件中添加ProgressBar

1. 一次性解决方案

使用RelativeLayout作为根布局,并使用android:layout_belowProgressBar和主要内容保持在工具栏下方。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimaryDark"/>

    <fr.castorflex.android.smoothprogressbar.SmoothProgressBar
        android:id="@+id/loadProgressBar"
        style="@style/LoadProgressBar"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_below="@+id/toolbar"
        android:indeterminate="true"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:orientation="vertical">
        <!-- Your Content here -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Content Text"/>

    </LinearLayout>

</RelativeLayout>

现在您可以在ActivitiyonCreate方法中访问ToolbarProgressBar
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    progressBar = (SmoothProgressBar) findViewById(R.id.loadProgressBar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
    }
}

2. 使用include的通用解决方案

更通用的方法是将ToolbarProgressBar放在单独的XML布局文件中,并在活动布局中包含它们。

toolbar.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimaryDark"/>

    <fr.castorflex.android.smoothprogressbar.SmoothProgressBar
        android:id="@+id/loadProgressBar"
        style="@style/LoadProgressBar"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_below="@+id/toolbar"
        android:indeterminate="true"/>

</merge>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <include
        layout="@layout/toolbar"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toolbar"
        android:orientation="vertical">
        <!-- Your Content here -->
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Content Text"/>

    </LinearLayout>

</RelativeLayout>

1
这是我在我的应用程序中正在做的事情。我只是想知道是否有一种方法可以直接将其添加到工具栏中。 - ariets
1
我认为没有通过添加View到“Toolbar”来实现这一点的方式。 “Toolbar”是一个“ViewGroup”,但在“ViewGroup”的子项布局之前,“NavigationIcon”、“Title”和“Subtitle”会在此ViewGroup的左侧进行布局,并且在右侧菜单获得其空间。这是我的结果,通过设置“params.gravity = Gravity.FILL_HORIZONTAL”:Toolbar Image - David Boho
@DavidWiesner:在较新版本的安卓系统中,当在Android Studio中使用时,仅使用具有XML模式的合并标记是不够的。android.support.v7.widget.Toolbar也需要有XML模式。 - Manish M Demblani

1
这将在工具栏下方显示水平工具栏:
<RelativeLayout 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.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">

        <android.support.v7.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <me.zhanghai.android.materialprogressbar.MaterialProgressBar
            android:layout_width="match_parent"
            android:layout_height="4dp"
            android:indeterminate="true"
            app:mpb_progressStyle="horizontal"
            app:mpb_useIntrinsicPadding="false"
            android:layout_alignParentBottom="true"
            style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal" />

    </RelativeLayout>

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

你可以访问我的Github获取更多信息工具栏下的水平进度条,或观看这个Youtube教程带有水平进度条的工具栏

0
试试这个,会显示在statusBar下面。
<androidx.coordinatorlayout.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">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:elevation="0dp"
    android:elevation="0dp">
    <ProgressBar
        android:id="@+id/progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        android:max="100"
        android:layout_marginTop="-7dp"
        android:layout_marginBottom="-7dp"
        android:visibility="visible" />
    <androidx.appcompat.widget.Toolbar

为什么将顶部和底部边距设置为-7dp? - Big McLargeHuge

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接