关注 安卓007 ,免费获取全套安卓开发学习资料

什么是SeekBar

SeekBar是支持拖动的进度显示条.

基础样例

1. 普通样例

效果图

安卓开发入门教程-UI控件_SeekBar_android

代码

  • 布局文件
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="50" />

  • activity代码
//设置进度
seekBar.progress = 60

2. 分段可拖动进度条样例

效果图

安卓开发入门教程-UI控件_SeekBar_布局文件_02

代码

  • 布局文件
<SeekBar
android:id="@+id/discreteSeekBar"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:progress="3" />

基础样例完整源代码

​https://gitee.com/cxyzy1/SeekBarDemo​

常用属性说明

属性名

用途

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

android:max

对于分段可拖动进度条,设置分段数

更多属性及实际效果,可以在开发工具里自行体验.



安卓开发入门教程系列汇总

开发语言学习

Kotlin语言基础

UI控件学习系列

UI控件_TextView

UI控件_EditText

UI控件_Button

UI控件_ImageView

UI控件_RadioButton

UI控件_CheckBox

UI控件_ProgressBar