Android 下方带有水平进度条的工具栏
随着Android棒棒糖和AppCompat-v7中新的工具栏API的出现,他们正在删除许多自动功能,以使工具栏/操作栏更加健壮。其中之一就是进度条。由于工具栏只是一个视图组,所以我认为添加ProgressBar很简单。然而,我似乎无法让它正常工作 我已完成以下操作(使用SmoothProgressBar库): 我原以为这会奏效,因为我只是在视图组的底部添加了一个ProgressBar。但是,它根本没有显示,正在删除标题。下面你可以看到一个前和后。有人知道如何着手解决这个问题吗?我的目标是在actionbar下面有一个ProgressBar 之前 之后Android 下方带有水平进度条的工具栏,android,android-appcompat,android-toolbar,Android,Android Appcompat,Android Toolbar,随着Android棒棒糖和AppCompat-v7中新的工具栏API的出现,他们正在删除许多自动功能,以使工具栏/操作栏更加健壮。其中之一就是进度条。由于工具栏只是一个视图组,所以我认为添加ProgressBar很简单。然而,我似乎无法让它正常工作 我已完成以下操作(使用SmoothProgressBar库): 我原以为这会奏效,因为我只是在视图组的底部添加了一个ProgressBar。但是,它根本没有显示,正在删除标题。下面你可以看到一个前和后。有人知道如何着手解决这个问题吗?我的目标是在ac
最简单的方法是直接在XML布局文件中添加
ProgressBar
1.一次性解决方案
使用RelativeLayout
作为根目录,并使用下面的android:layout_
将ProgressBar
和主要内容保留在工具栏下面
<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>
2.使用的通用解决方案包括
更通用的方法是将工具栏
和ProgressBar
放在单独的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>
<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>
活动\u main.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>
<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>
这将显示工具栏下方的水平工具栏:
<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>
或者观看这个youtube教程试试这个,它将显示在状态栏下
<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
这就是我在我的应用程序中所做的。我只是想有一种方法可以直接将它添加到工具栏。我不认为有任何方法可以通过向工具栏添加视图来实现这一点。“工具栏”是一个“视图组”,但在“视图组”的子项布局之前,导航图标
,标题
和副标题
在该视图组的左侧获得布局,在右侧菜单获得其空间。下面是我通过设置params.gravity=gravity.FILL_HORIZONTAL
:得到的结果@DavidWiesner:在android的较新版本中,当在android Studio中使用时,合并标记单独使用xml模式是没有帮助的。android.support.v7.widget.Toolbar还需要xml模式,为什么将上下边距设置为-7dp?