五、ProgressBar(进度条)
max:进度条的最大值
progress:进度条已完成进度值
indeterminate:如果设置成true,则进度条不精确显示进度
style="?android:attr/progressBarStyleHorizontal":水平进度条
ui页面源码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?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> |
后台代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | 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); } } |
效果图片
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通