文章目录
- 常用属性
- 项目结构
- 主要代码
常用属性
max
:进度条的最大值progress
:进度条已完成进度值indeterminate
:如果设置成true
,则进度条不精确显示进度style="?android:attr/progressBarStyleHorizontal"
:水平进度条
项目结构
主要代码
mainActivity.java
package com.study.myprogressbar;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private ProgressBar progressBar1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.pb);
progressBar1 = findViewById(R.id.pb1);
}
public void leoClick(View view) {
//GONE:隐藏,VISIBLE:显示
if (progressBar.getVisibility() == View.GONE) {
progressBar.setVisibility(View.VISIBLE);
} else {
progressBar.setVisibility(View.GONE);
}
}
public void load(View view) {
int progress = progressBar1.getProgress(); //获取当前的进度值
progress += 10; //每点一下加10
progressBar1.setProgress(progress);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:id="@+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="leoClick"
android:text="显示隐藏进度条" />
<ProgressBar
android:id="@+id/pb1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:max="100" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="load"
android:text="模拟下载" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:indeterminate="true"
android:max="100" />
</LinearLayout>