1. ProgressBar类
ProgressBar用于显示进度条,进度条有两种不同的样式,旋转和水平,通过设置style来区分。
- 旋转进度条,有大、中、小三种样式。
style="?android:attr/progressBarStyleSmall" style="?android:attr/progressBarStyleSmallInverse" style="?android:attr/progressBarStyleSmallTitle" style="?android:attr/progressBarStyleLarge" style="?android:attr/progressBarStyleLargeInverse" style="?android:attr/progressBarStyleInverse" - 水平进度条,水平进度条可以设置
progress、secondaryProgress、max属性,来显示进度。
效果如下<ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:max="100" android:progress="30"/> <ProgressBar android:layout_width="match_parent" android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" android:max="100" android:progress="40" android:secondaryProgress="70"/>

2. 自定义进度条背景
- 可以通过设置
shape文件来指定进度条背景,需要配置progressDrawable属性<ProgressBar android:layout_width="match_parent" android:layout_height="5dp" style="?android:attr/progressBarStyleHorizontal" android:max="100" android:progress="30" android:progressDrawable="@drawable/progress_bar_bg"/> <ProgressBar android:layout_width="match_parent" android:layout_height="5dp" android:layout_marginTop="5dp" style="?android:attr/progressBarStyleHorizontal" android:max="100" android:progress="40" android:secondaryProgress="70" android:progressDrawable="@drawable/progress_bar_bg"/>progress_bar_bg.xml文件
效果如下<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background" > <shape> <solid android:color="#ffffffff" /> </shape> </item> <item android:id="@android:id/secondaryProgress" > <clip> <shape> <solid android:color="#fff84c00" /> </shape> </clip> </item> <item android:id="@android:id/progress" > <clip> <shape> <solid android:color="#ffff8c00" /> </shape> </clip> </item> </layer-list>

- 利用图片指定进度条背景
<ProgressBar android:layout_width="match_parent" android:layout_height="10dp" style="?android:attr/progressBarStyleHorizontal" android:max="100" android:progress="30" android:progressDrawable="@drawable/progress_bar_star_bg"/>progress_bar_star_bg.xml文件
效果如下<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@android:id/background" > <shape> <solid android:color="#ffffffff" /> </shape> </item> <item android:id="@android:id/progress" android:drawable="@drawable/star" /> </layer-list>

- 利用
indeterminateDrawable属性<ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" android:indeterminateDrawable="@anim/anim_progress_bar" android:indeterminateDuration="1000"/>anim_progress_bar.xml文件
效果如下<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/nav_refresh" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" />

相关文章
Android ProgressBar控件
Android SeekBar控件
Android RatingBar控件

本文详细介绍了Android中的ProgressBar控件,包括其两种样式:旋转和水平,以及如何通过设置style属性来区分。此外,还讲解了如何自定义进度条背景,利用shape文件和图片指定背景,以及使用indeterminateDrawable属性实现动画效果。
1624

被折叠的 条评论
为什么被折叠?



