一、预览

ProgressBar的使用很简单,先看一下效果图

二、layout布局

就放了两个progressBar和两个Button

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center_horizontal"    android:orientation="vertical"    tools:context=".MainActivity">    <TextView        android:layout_marginTop="50dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="圆形加载样式progressBar"/>                默认的样式是圆形加载样式    <ProgressBar        android:id="@+id/progressBar1"        android:layout_width="match_parent"        android:visibility="gone"  先让它隐藏,代码控制显示        android:layout_height="wrap_content"/>    <Button        android:id="@+id/btn1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="显示"/>    <TextView        android:layout_marginTop="50dp"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="进度条样式progressBar"/>    <ProgressBar        android:id="@+id/progressBar2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:visibility="gone"  先让它隐藏,代码控制显示        style="?android:attr/progressBarStyleHorizontal"  设置为水平进度条样式        android:max="100"  设置进度条的最大值为100        />    <Button        android:id="@+id/btn2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="显示"/></LinearLayout>

三、代码控制progressBar的显示和隐藏

public class MainActivity extends AppCompatActivity implements View.OnClickListener{    private Button btn1,btn2;    private ProgressBar progressBar1,progressBar2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn1 = findViewById(R.id.btn1);        btn2 = findViewById(R.id.btn2);        progressBar1 = findViewById(R.id.progressBar1);        btn1.setOnClickListener(this);        progressBar2 = findViewById(R.id.progressBar2);        btn2.setOnClickListener(this);    }    @Override    public void onClick(View view) {        switch (view.getId()){            //显示圆形加载样式progressBar            case R.id.btn1:            //判断ProgressBar当前是否可见,并且设置其是否可见            //View.VISIBLE是显示,View.INVISIBLE是隐藏但占位置,View.GONE是消失不占位置                if (progressBar1.getVisibility()==View.GONE){                    progressBar1.setVisibility(View.VISIBLE);                }else {                    progressBar1.setVisibility(View.GONE);                }                break;            //显示进度条样式progressBar            case R.id.btn2:                progressBar2.setVisibility(View.VISIBLE);                int progress = progressBar2.getProgress();//获取当前进度                progress += 10;                progressBar2.setProgress(progress); //重新设置进度                if(progress > 100){                    progressBar2.setVisibility(View.GONE);                }                break;             default:                 break;        }    }}

更多相关文章

  1. Android百度地图开发之显示当前位置地图
  2. android键盘谈起和收起监听
  3. Android实现随机圆形云标签效果
  4. Android中用BaseAdapter实现带Button和显示网络图片的Listview
  5. Android(安卓)常用基本控件 ImageButton
  6. onPrepareOptionsMenu 和onCreateOptionsMenu 的区别
  7. Android经常使用UI组件 - TextView
  8. android多图拼接长图并合理显示
  9. Android(安卓)中Webview 自适应屏幕

随机推荐

  1. 监听Android应用程序是否卸载成功
  2. android圆角矩形的实现
  3. virtualbox 安装android设置分辨率(自定
  4. 【Android】Android(安卓)Studio百问
  5. [Android] 环境配置之Android(安卓)Studi
  6. Android(安卓)framework系统手势
  7. android:TextAppearance.Material.Widget
  8. Android(安卓)jni知识点
  9. android 注册、登录实现程序
  10. Android中配置单元测试