目录
常用属性详解
1.android:max 进度条的最大值
2.android:progress 进度条已完成进度值
3.android:indeterminate 如果设置成true,则进度条不精确显示进度
4.style="?android:attr/progressBarStyleHorizontal" 水平进度条
案例
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ProgressBar
android:id="@+id/pb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="显示隐藏进度条"
android:onClick="FinnyOnClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/pb2"
android:max="100"
android:progress="0"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
<Button
android:text="模拟下载+"
android:onClick="load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="模拟下载-"
android:onClick="download"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/pb3"
android:max="100"
android:indeterminate="true"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
package com.example.progressbar;
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 progressbar2;
private ProgressBar progressbar3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressbar = findViewById(R.id.pb1);
progressbar2 = findViewById(R.id.pb2);
progressbar3 = findViewById(R.id.pb3);
}
public void FinnyOnClick(View view) {
//如果progressbar是隐藏的
if(progressbar.getVisibility() == View.GONE){
//让他显示出来
progressbar.setVisibility(View.VISIBLE);
}else{
//如果不是隐藏的,让他隐藏
progressbar.setVisibility(View.GONE);
}
//水平进度条
if(progressbar2.getVisibility() == View.GONE){
progressbar2.setVisibility(View.VISIBLE);
}else{
progressbar2.setVisibility(View.GONE);
}
//水平进度条,不精确显示进度的进度条
if(progressbar3.getVisibility() == View.GONE){
progressbar3.setVisibility(View.VISIBLE);
}else{
progressbar3.setVisibility(View.GONE);
}
}
public void load(View view) {
int progress = progressbar2.getProgress();
progress += 10;
progressbar2.setProgress(progress);
}
public void download(View view) {
int progress = progressbar2.getProgress();
progress -= 10;
progressbar2.setProgress(progress);
}
}