max:進度條的最大值
progress:進度條已完成進度值
indeterminate:如果設置成true,則進度條不精確顯示進度
style="?android:attr/progressBarStyleHorizontal":水平進度條
ui頁面源碼示例
<?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/Pro" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <!--顯示或者隱藏進度條--> <Button android:text="顯示隱藏進度條" android:onClick="leoClink" android:layout_width="150dp" android:layout_height="50dp"/> <!--添加水平進度條--> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:id="@+id/ProShuPing" android:max="100" android:layout_width="300dp" android:layout_height="wrap_content"/> <Button android:text="模擬下載" android:onClick="leoLoad" android:layout_width="150dp" android:layout_height="50dp"/> <!--不精確顯示進度--> <ProgressBar style="?android:attr/progressBarStyleHorizontal" android:max="100" android:layout_width="300dp" android:indeterminate="true" android:layout_height="wrap_content"/> </LinearLayout>
后台代碼示例
package com.example.myprogressbae; 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 progressBarLoad; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar = findViewById(R.id.Pro); progressBarLoad = findViewById(R.id.ProShuPing); } //顯示隱藏進度條 public void leoClink(View view) { if (progressBar.getVisibility()==View.GONE) { progressBar.setVisibility(View.VISIBLE); } else { progressBar.setVisibility(View.GONE); } } //模擬下載 public void leoLoad(View view) { int progress = progressBarLoad.getProgress(); progress+=10; progressBarLoad.setProgress(progress); } }
效果圖片