文章目录

介绍

ProgressBar 是 Android 下的进度条,也是为数不多的直接继承于 View 类的控件,直接子类有 AbsSeekBar 和 ContentLoadingProgressBar,其中 AbsSeekBar 的子类有 SeekBar 和 RatingBar。

先用起来

【达内课程】ProgressBar_progressbar
activity_main

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0" />

<TextView
android:id="@+id/tv_show_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0/100" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Button" />
</LinearLayout>

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ProgressBar progressBar;
private Button button;
private TextView textView;
private int progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

progressBar = findViewById(R.id.progressBar);
button = findViewById(R.id.button);
textView = findViewById(R.id.tv_show_progress);
button.setOnClickListener(this);
}

@Override
public void onClick(View view) {
progress += 5;
progressBar.setProgress(progress);
textView.setText(progressBar.getProgress() + "/" + progressBar.getMax());
}
}

可以看到 ProgressBar 的最大值是 100,即使是设置 120,也是显示 100。

【继承结构】
View
——ProgressBar

【核心属性】
​​​style​​​:进度条的显示样式,默认是圆形,如果需要水平的线性进度条,必须设置​​style​​​属性值,例如取值为:​​style="?android:attr/progressBarStyleHorizontal"​

​android:max​​:进度的最大值,取值为 int 类型数值

​android:progress​​:当前的进度值,取值为 int 类型数值

【核心方法】

void setMax(int max)
int getMax()
void setProgress(int progress)
int getProgress()

​如何让ProgressBar自动增长呢?可以戳这里看栗子​

设进度条背景

【达内课程】ProgressBar_android_02

在 drawable 下新增 progressbar.xml

<?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>
<corners android:radius="5dip" />
<gradient
android:angle="0"
android:centerColor="#ff5a5d5a"
android:centerY="0.75"
android:endColor="#ff747674"
android:startColor="#ff9d9e9d" />
</shape>
</item>

<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="0"
android:centerColor="#80ffb600"
android:centerY="0.75"
android:endColor="#a0ffcb00"
android:startColor="#80ffd300" />
</shape>
</clip>
</item>

<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dip" />
<gradient
android:angle="0"
android:endColor="#8000ff00"
android:startColor="#80ff0000" />
</shape>
</clip>
</item>

</layer-list>

代码中使用

<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="@drawable/progressbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0"/>

分享一个下载进度条:FlickerProgressBar

效果图:
【达内课程】ProgressBar_android_03
​​​代码分析​​​​Github地址​