在程序开发中,ProgressBar提供了可以向用户展示当前任务的进度。

1. 在学习之前,我们先了解一些相关知识,查看api文档 progressBar。一般最常用的方法有以下几点:

getMax() 得到最大刻度 getProgress() 得到刻度 setMax(int max) 设置最大刻度 setProgress(int progress) 设置刻度 setSecondaryProgress(int secondaryProgress) 设置第二刻度 setVisibility(int v) 设置是否可见

2. 下面我们就来练习一下刻度条的使用方法

1. 布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="小圆形进度条" />    <ProgressBar        style="?android:attr/progressBarStyleSmallTitle"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="中型圆形进度条" />    <ProgressBar        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="大型圆形进度条" />    <ProgressBar        style="?android:attr/progressBarStyleLarge"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="水平进度条" />    <ProgressBar        style="?android:attr/progressBarStyleHorizontal"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:max="100"        android:progress="30" />    <ProgressBar        android:id="@+id/progressbar"        style="?android:attr/progressBarStyleHorizontal"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:max="100"        android:progress="30"        android:secondaryProgress="60" />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="horizontal" >        <Button            android:id="@+id/button1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="20dp"            android:text="增加进度" />        <Button            android:id="@+id/button2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginTop="20dp"            android:text="减小进度" />    </LinearLayout></LinearLayout>

2. Java代码

package com.android.progressdemo;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.Window;import android.widget.Button;import android.widget.ProgressBar;public class ProgressDemo extends Activity implements OnClickListener {    private ProgressBar progressBar;    private Button button1, button2;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);               /*         * 如何设置窗口有刻度的效果         * 注意requestWindowFeature()方法必须写在setContentView()方法之前         */        requestWindowFeature(Window.FEATURE_PROGRESS);        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);        setContentView(R.layout.main);          setProgressBarVisibility(true);        setProgressBarIndeterminateVisibility(true);        setProgress(3500);                initComponenet();        button1.setOnClickListener(this);        button2.setOnClickListener(this);    }    private void initComponenet() {        progressBar = (ProgressBar) findViewById(R.id.progressbar);        button1 = (Button) findViewById(R.id.button1);        button2 = (Button) findViewById(R.id.button2);    }    @Override    public void onClick(View v) {        // TODO Auto-generated method stub        switch (v.getId()) {            case R.id.button1:                progressBar.setProgress((int) (progressBar.getProgress() * 1.2));                progressBar.setSecondaryProgress((int) (progressBar.getProgress() * 1.2));                break;            case R.id.button2:                progressBar.setProgress((int) (progressBar.getProgress() * 0.8));                progressBar.setSecondaryProgress((int) (progressBar.getProgress() * 0.8));                break;        }    }}

2. 程序运行结果






更多相关文章

  1. Android(安卓)复习巩固------ Animation 动画 (简介)
  2. Android(安卓)focus search returned a view that wasn't able t
  3. android中selector改变界面状态用法小结
  4. android TextView 改变边框
  5. android Configuration系统设置
  6. android EditText属性详解
  7. Android(安卓)RecycleView实现不同样式Item样式效果完美解决
  8. android设置屏幕方向与自动感应切换
  9. 这是我见过有关Android(安卓)RecyclerView最好的一篇文章:深入解

随机推荐

  1. MySQL如何支撑起亿级流量
  2. mysql修改sql_mode报错的解决
  3. 基于mysql中delete的语法别名问题
  4. MySQL 用 limit 为什么会影响性能
  5. 一次MySQL启动导致的事故实战记录
  6. MySQL中几种插入和批量语句实例详解
  7. MySQL 如何限制一张表的记录数
  8. MySQL into_Mysql中replace与replace int
  9. MYSQL 的10大经典优化案例场景实战
  10. MySQL 数据库定时备份的几种方式(全面)