布局
要想在布局文件中声明圆形进度条,可以添加 style 属性值为 style="?android:progressBarStyle"
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="@+id/pb_progress"
style="?android:progressBarStyle"
android:layout_width="match_parent"
android:layout_height="30dp"/>
</androidx.appcompat.widget.LinearLayoutCompat>
定义样式
使用 progressDrawable 定义进度条样式。
layer-list 表示这是一个层次列表,即层次图形定义,其下面再定义两个层次,背景层次的id定义为 @android:id/background,采用的是形状图形;进度条层次的id为 @android:id/progress,采用的事裁剪图形 ClipDrawable(节点名称为 clip)。
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<solid android:color="#333333"/>
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<nine-patch android:src="@color/snow"/>
</clip>
</item>
</layer-list>
设置进度条属性
class ProgressBarActivity:AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_progress)
val progressBar = findViewById<ProgressBar>(R.id.pb_progress)
// 设置最大进度
progressBar.max = 100
// 默认进度
progressBar.progress = 0
// 设置进度条图形
progressBar.progressDrawable = resources.getDrawable(R.drawable.layer_pb)
}
}









