当前位置: 首页> 房产> 市场 > 西宁刚刚发生的大事_互联网保险公司排行榜_百度关键词收录排名_游戏推广一个月能拿多少钱

西宁刚刚发生的大事_互联网保险公司排行榜_百度关键词收录排名_游戏推广一个月能拿多少钱

时间:2025/7/11 15:33:42来源:https://blog.csdn.net/rr_R_rr/article/details/142881410 浏览次数:0次
西宁刚刚发生的大事_互联网保险公司排行榜_百度关键词收录排名_游戏推广一个月能拿多少钱

ProgressBar

目录

ProgressBar

在XML中定义ProgressBar

ProgressBar风格样式

ProgressBar常用XML属性

在Java代码中控制ProgressBar

实例 


什么是ProgressBar?

ProgressBar是Android中的一个视图控件,主要用于表示一个任务的进度情况,比如文件下载的进度、音乐播放的进度等。它通常以水平条或圆圈的形式展示,允许用户了解任务的当前状态和进度百分比。

如何在Android中使用ProgressBar?

在Android中,可以通过XML布局文件或者Java代码来创建和配置ProgressBar。

在XML中定义ProgressBar

<ProgressBarandroid:id="@+id/progressBar"                        android:layout_width="wrap_content"                  android:layout_height="wrap_content"style="@android:style/Widget.ProgressBar.Horizontal" android:max="100"                                    android:progress="0" />                              
  • android:id: 设置ProgressBar的唯一标识符。
  • android:layout_widthandroid:layout_height: 设置ProgressBar的宽度和高度。
  • style="?android:attr/progressBarStyleHorizontal": 设置ProgressBar的样式为水平进度条。
  • android:max: 设置ProgressBar的最大值。
  • android:progress: 设置ProgressBar的当前进度值。

ProgressBar风格样式

Android支持多种风格的进度条,通过style属性可以为ProgressBar指定风格。该属性可支持如下几个属性值:

@android:style/Widget.ProgressBar.Horizontal:水平进度条。@android:style/Widget.ProgressBar.Inverse:普通大小的环形进度条。@android:style/Widget.ProgressBar.Large:大环形进度条。@android:style/Widget.ProgressBar.Large.Inverse:大环形进度条。@android:style/Widget.ProgressBar.Small:小环形进度条。@android:style/Widget.ProgressBar.Small.Inverse:小环形进度条。

在Android开发中,ProgressBar的样式设定有两种方式,另一种可以通过如下方式使用:

?android:attr/progressBarStyle?android:attr/progressBarStyleHorizontal?android:attr/progressBarStyleInverse?android:attr/progressBarStyleLarge?android:attr/progressBarStyleLargeInverse?android:attr/progressBarStyleSmall?android:attr/progressBarStyleSmallInverse?android:attr/progressBarStyleSmallTitle

ProgressBar常用XML属性

android:max:进度条的最大值
android:progress:进度条已完成进度值
android:progressDrawable:设置轨道对应的Drawable对象
android:indeterminate:如果设置成true,则进度条不精确显示进度
android:indeterminateDrawable:设置不显示进度的进度条的Drawable对象
android:indeterminateDuration:设置不精确显示进度的持续时间
android:secondaryProgress:二级进度条,类似于视频播放的一条是当前播放进度,一条是缓冲进度,前者通过progress属性进行设置!

在Java代码中控制ProgressBar

然后,在Activity或Fragment的Java代码中,可以通过findViewById方法获取ProgressBar的实例,并根据需要更新其进度:

public class MainActivity extends AppCompatActivity {private ProgressBar progressBar;private ProgressBar progressBar2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);EdgeToEdge.enable(this);setContentView(R.layout.activity_main);ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);return insets;});progressBar=findViewById((R.id.pb));progressBar2=findViewById((R.id.pb2));}public void leoClick(View view){if(progressBar.getVisibility()==view.GONE){progressBar.setVisibility((View.VISIBLE));}else{progressBar.setVisibility(View.GONE);}}public void load(View view){int progress = progressBar2.getProgress();progress+=10;progressBar2.setProgress(progress);}
}
  • ProgressBar progressBar = findViewById();: 通过findViewById方法获取ProgressBar的实例。
  • progressBar.setProgress();: 在UI线程中更新ProgressBar的当前进度。如果ProgressBar是水平的,进度值是一个0到100的整数;如果是圆形的,进度值是一个0到10000的整数。
  • setVisibility();:控制ProgressBar的显示与隐藏。

需要注意的是,如果进度是由一个任务的执行进度决定的,为了避免UI线程阻塞,应该将该任务放在子线程中执行,然后使用runOnUiThread()方法在UI线程中更新ProgressBar的进度。

实例 

自定义控件,要求有模糊模式(圆形)的多种样式;模糊模式(水平)且点击按钮进度条消失;精确模式(水平)且点击按钮进度条进度增加10%;精确模式(水平)且进度条进度为18%。

XML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><ProgressBarstyle="@android:style/Widget.ProgressBar.Small"android:layout_width="wrap_content"android:layout_height="wrap_content" /><ProgressBarstyle="@android:style/Widget.ProgressBar.Large"android:layout_width="wrap_content"android:layout_height="wrap_content" /><ProgressBarandroid:id="@+id/pb"style="?android:attr/progressBarStyleHorizontal"android:layout_width="300dp"android:layout_height="wrap_content" /><Buttonandroid:text="显示隐藏进度条"android:onClick="leoClick"android:layout_width="wrap_content"android:layout_height="wrap_content" /><ProgressBarandroid:id="@+id/pb2"style="?android:attr/progressBarStyleHorizontal"android:max="100"android:layout_width="300dp"android:layout_height="wrap_content" /><Buttonandroid:text="模拟下载"android:onClick="load"android:layout_width="wrap_content"android:layout_height="wrap_content" /><ProgressBarstyle="?android:attr/progressBarStyleHorizontal"android:max="100"android:layout_width="300dp"android:indeterminate="true"android:layout_height="wrap_content" /><ProgressBarstyle="@android:style/Widget.ProgressBar.Horizontal"android:layout_width="match_parent"android:layout_height="wrap_content"android:max="100"android:progress="18" /></LinearLayout>

JAVA代码

package com.example.myprogressbar;import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;public class MainActivity extends AppCompatActivity {private ProgressBar progressBar;private ProgressBar progressBar2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);EdgeToEdge.enable(this);setContentView(R.layout.activity_main);ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);return insets;});progressBar=findViewById((R.id.pb));progressBar2=findViewById((R.id.pb2));}public void leoClick(View view){if(progressBar.getVisibility()==view.GONE){progressBar.setVisibility((View.VISIBLE));}else{progressBar.setVisibility(View.GONE);}}public void load(View view){int progress = progressBar2.getProgress();progress+=10;progressBar2.setProgress(progress);}
}

 运行截图

                   

                                              (点击按钮隐藏/显示进度条)       (点击按钮增加进度)

关键字:西宁刚刚发生的大事_互联网保险公司排行榜_百度关键词收录排名_游戏推广一个月能拿多少钱

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: