android:max=”100” 进度条最大值
android:progress=”50” 进度条已完成进度值
android:indeterminate=”true” 如果设置为true,进度条不显示进度
style=”?android:attr/progressBarStyleHorizontal” 水平进度条
前端代码示例
<?xml version=”1.0” encoding=”utf-8”?>
android:layout_height=”match_parent”
android:orientation=”vertical”
xmlns:android=”http://schemas.android.com/apk/res/android">
<ProgressBar<br /> android:id="@+id/pd"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> /><br /> <Button<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="显示隐藏进度"<br /> android:onClick="dataClick"<br /> /><br /> <ProgressBar<br /> android:id="@+id/pd2"<br /> style="?android:attr/progressBarStyleHorizontal"<br /> android:max="100"<br /> android:layout_width="300dp"<br /> android:layout_height="wrap_content"/><br /> <Button<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="模拟下载"<br /> android:onClick="loadClick"<br /> /><br /> <ProgressBar<br /> android:indeterminate="true"<br /> style="?android:attr/progressBarStyleHorizontal"<br /> android:max="100"<br /> android:layout_width="300dp"<br /> android:layout_height="wrap_content"/><br /></LinearLayout>
后端测试示例
package com.example.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 progressBar2;<br /> @Override<br /> protected void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout._activity_main_);<br /> progressBar=findViewById(R.id._pd_);<br /> progressBar2=findViewById(R.id._pd2_);<br /> }
public void dataClick(View view) {<br /> if(progressBar.getVisibility()==View._GONE_){<br /> progressBar.setVisibility(View._VISIBLE_);<br /> }else {<br /> progressBar.setVisibility(View._GONE_);<br /> }
}
public void loadClick(View view) {<br /> int progress = progressBar2.getProgress();<br /> progress+=10;<br /> progressBar2.setProgress(progress);<br /> }<br />}