目录
一、ProgressBar是什么?
ProgressBar控件的参数详解如下:
-
android:id
:控件的唯一标识符,用于在布局文件或代码中引用该控件。 -
android:layout_width
:控件的宽度,可以设置为具体数值(如20dp
)、match_parent
(填充父容器的宽度)或wrap_content
(根据内容自适应宽度)。 -
android:layout_height
:控件的高度,可以设置为具体数值(如20dp
)、match_parent
(填充父容器的高度)或wrap_content
(根据内容自适应高度)。 -
android:indeterminate
:指定是否为不确定型进度条。取值为true
表示不确定型,取值为false
表示确定型。 -
android:max
:设定进度条的最大值,默认为100。通过该属性可以设置进度条的整体范围。 -
android:progress
:设置当前的进度值。默认为0,表示进度条的初始状态。 -
android:secondaryProgress
:设置次要进度的值,用于双重进度条的情况。默认为0。 -
android:minWidth
:设定进度条的最小宽度。 -
android:minHeight
:设定进度条的最小高度。 -
android:progressDrawable
:设置自定义的进度条样式。可以指定一个.drawable类型的资源文件作为进度条的背景样式。 -
android:indeterminateDrawable
:设置自定义的不确定型进度条样式。可以指定一个.drawable类型的资源文件作为不确定型进度条的背景样式。 -
android:indeterminateDuration
:设置不确定型进度条动画的循环周期时长,默认为2700毫秒。 -
android:indeterminateOnly
:当设定了indeterminate
为true时,通过该属性可以指定进度条只显示不确定样式,而不显示具体进度。
这些参数可以通过在布局文件中设置相关属性来自定义ProgressBar控件的外观和行为。根据具体需求,可以根据需要调整这些参数以满足设计要求。
二、使用步骤
1.demo
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DictionaryTableActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="26dp"
tools:layout_editor_absoluteY="0dp">
<Button
android:id="@+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="button"
tools:ignore="MissingConstraints" />
<ProgressBar
android:id="@+id/progress_circular"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:max="100"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
public class DictionaryTableActivity extends AppCompatActivity implements View.OnClickListener {
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dictionary_table);
Button button = (Button) findViewById(R.id.button_2);
progressBar = (ProgressBar) findViewById(R.id.progress_circular);
button.setOnClickListener(this);
}
@SuppressLint("NonConstantResourceId")
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button_2:
int process = progressBar.getProgress();
progressBar.setProgress(process+10);
break;
default:
break;
}
}
}
2.ProgressBar方法
setMax(int max)
:设置进度条的最大值。setProgress(int progress)
:设置当前的进度值。setSecondaryProgress(int secondaryProgress)
:设置次要进度的值,用于双重进度条。incrementProgressBy(int diff)
:增加指定的进度值。incrementSecondaryProgressBy(int diff)
:增加指定的次要进度值。setIndeterminate(boolean indeterminate)
:设置是否为不确定型进度条。setVisibility(int visibility)
:设置进度条的可见性。