什么是ProgressBar
ProgressBar是用于提示用户进行等待的UI控件,.
基础样例
1.loading图
效果图
代码
布局文件代码
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
activity代码
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//隐藏进度条
progressBar.visibility = View.GONE
//显示进度条
progressBar.visibility = View.VISIBLE
}
}
2.横向进度条
效果图
代码
布局文件代码
android:id="@+id/horizontalProgressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="20" />
activity代码
//横向进度条:设置进度
horizontalProgressBar.progress = 50
3.横向loading图
效果图
代码
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:indeterminate="true" />
基础样例完整源代码
常用属性说明
属性名
用途
android:layout_width
设置控件宽度,可设置为:match_parent(和父控件一样),wrap_content(按照内容自动伸缩),设置固定值(如200dp)
android:layout_height
设置控件高度,可设置为:match_parent(和父控件一样),wrap_content(按照内容自动伸缩),设置固定值(如200dp)
android:gravity
控件内对齐方式
android:background
设置背景,可以是色值(如#FF0000)或图片等
android:visibility
可选值: visible(显示), invisible(隐藏,但是仍占据UI空间),gone(隐藏,且不占UI空间)
android:progress
设置进度(对横向进度条有用),取值范围:0-100
style
设置显示样式.
"?android:attr/progressBarStyle":转圈loading图;
"?android:attr/progressBarStyleHorizontal":横向进度条;
"?android:attr/progressBarStyleHorizontal":横向loading图;
更多属性及实际效果,可以在开发工具里自行体验.