Material Design ProgressBar
with consistent appearance on Android 4.0+.
ProgressBar
.ProgressBar
.ProgressBar
.Android 4.4.4
Samsung Android 5.0.1 (native implementation fails to tint)
Android 6.0.1
Gradle:
implementation 'me.zhanghai.android.materialprogressbar:library:1.6.1'
You can either simply use the MaterialProgressBar
widget, or set drawables from this library on a regular ProgressBar
.
Simply replace your ProgressBar
with MaterialProgressBar
, and remember to apply corresponding style and attribute for correct behavior.
For example, to create an indeterminate horizontal MaterialProgressBar
:
<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
app:mpb_progressStyle="horizontal"
style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal" />
In order to make your ProgressBar
take the correct and consistent size on all versions, you will always need to use one of the styles from this library. The trick inside it is android:minWidth
, android:maxWidth
(and height) that controls the drawable size.
Widget.MaterialProgressBar.ProgressBar
Widget.MaterialProgressBar.ProgressBar.Horizontal
Available custom attributes:
app:mpb_progressStyle
: Style of progress drawable: circular
or horizontal
. Defaults to circular
.app:mpb_setBothDrawables
: Whether both determinate and indeterminate drawables should be set on this progress bar. Defaults to false
(for performance). Should be set to true
if you want to use both of the backported determinate and indeterminate drawables.app:mpb_useIntrinsicPadding
: Whether progress drawable should use its intrinsic padding. Defaults to true
.app:mpb_showProgressBackground
: Whether progress drawable should show a progress background. Defaults to true
for horizontal progress drawable, false
otherwise.app:mpb_determinateCircularProgressStyle
: Style of determinate circular progress drawable: normal or dynamic. Defaults to normal
.8 tint-related attributes such as app:mpb_progressTint
and app:mpb_progressTintMode
are also supported so that they can control the tinting of progress drawables. The default tint color is ?colorControlActivated
, and the default tint mode is src_in
.
For a detailed example, you can refer to the sample app's layout, where you can find examples such as removing progress padding or background.
Three Material Design drawables are backported to Android 4.0 (API 14), so you can create one and set it directly on your ProgressBar
.
HorizontalProgressDrawable
IndeterminateHorizontalProgressDrawable
CircularProgressDrawable
IndeterminateCircularProgressDrawable
For example, to set a IndeterminateHorizontalProgressDrawable
on a ProgressBar
.
progressBar.setIndeterminateDrawable(new IndeterminateHorizontalProgressDrawable(this));
You will also need to set a style from this library as in the section above.
For example, to define an indeterminate horizontal ProgressBar
.
<ProgressBar
android:id="@+id/indeterminate_horizontal_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true"
style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal" />
Don't forget to create and set the drawable as above.
You can also customize the behavior of these drawables by calling setShowBackground()
and setUseIntrinsicPadding()
. Tint-related methods setTint()
, setTintList()
and setTintMode()
are also backported so that you can use them directly, but remember to take these drawables as their actual type or TintableDrawable
because VM won't be able to find these methods on Drawable
for legacy platforms.
If you want to support API level < 18, you'll need to workaround a canvas limitation as in here; Using MaterialProgressBar
does this automatically.
For a detailed example, you can refer to the onCreate()
method of the old sample's MainActivity
and its layout main_activity.xml.
The AAR of this library has already included a ProGuard configuration file to make ObjectAnimator
work properly.
Neither Support v4 nor AppCompat v7 backported animation API to versions prior to ICS, and the NineOldAndroids library has already been deprecated since people should all be using minSdkVersion="14"
now, so versions older than ICS are not supported.
Copyright 2015 Hai Zhang
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Android L的ProgressBar 默认效果就非常好,看了网易新闻 在5.0 和4.4上分别装入网易新闻app 发现网易用的ProgressBar就是默认样式,但是在两个版本上显示不同。搜索了很多资料都是引用个android:indeterminateDrawable="@drawable/xxx"自定义文件,引用这个文件后ProgressBar 颜色是变了但没有5.0的
关键代码: android:indeterminateTint="@color/btn_orange" android:indeterminateTintMode="src_atop" <LinearLayout android:id="@+id/pblayout" android:layout_width="match_parent" android:layout_hei
1、简单粗暴 style="?android:attr/progressBarStyleHorizontal" (跑个题:@resource的方式可以获取到当前应用主题下的 style 属性值,which一般可以在属性文件夹 values 下找到;而?android或者?attr这种,表示从Theme中查找引用的资源名,经常用在多主题时的场景,属性值会随着主题而改变。) 2、自己写style
关于Android如何改变ProgressBar进度条颜色 基本原理是在drawable目录下建立xml文件来描述背景图 android:drawable="@drawable/secondary" /> 然后通过设置progressDrawable属性设置