Android界面编程之六 第五组UI组件:ProgressBar及其子类

ProgressBar本身代表了进度条组件,它还派生了两个常用组件:SeekBar和RatingBar。
它和其子类在用法上十分相似,只是显示界面上有区别
2.6.1 ProgressBar的功能与用法
Android支持几种风格的进度条,通过style属性可以为ProgressBar指定风格。其支持的属性有:
@android:syle/Widget.ProgressBar.Horizontal
水平进度条
@android:syle/Widget.ProgressBar.Inverse
普通大小的环形进度条
@android:syle/Widget.ProgressBar.Large
大环形进度条
@android:syle/Widget.ProgressBar.Large.Inverse
大环形进度条
@android:syle/Widget.ProgressBar.Small
小环形进度条
@android:syle/Widget.ProgressBar.Small.Inverse
小环形进度条
除此之外,ProgressBar还支持如下常用XML属性:
android:max
设置该进度条的最大值
android:progress
设置该进度条的已完成进度
android:progressDrawable
设置该进度条的轨道对应的Drawable对象
android:indeterminate
若设为true,则进度条为不精确显示进度
android:indeterminateDrawable
设置绘制不显示进度条的Drawable对象
android:indeterminateDuration
设置不精确显示进度条的持续时间
android:progressDrawable用于指定进度条的轨道绘制形式,该属性可指定为一个LayerDrawable对象的引用(该对象可通过在XML文件中用<layer-list元素进行配置>)
setProgress(int)
设置进度的完成百分比
incrementProgressBy(int)
设置进度条的进度增加或减少(正负以区分)
示例代码:
public class MainActivity extends Activity {

private int [] data = new int [100];
int hasData = 0;
int status = 0;
ProgressBar
bar , bar2 ;
Handler
mHandler = new Handler()
{
@Override
public void handleMessage(Message msg)
{
if (msg. what == 0x11)
{
bar .setProgress( status );
bar2 .setProgress( status );
}
}
};
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super .onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main );
       
bar = (ProgressBar) findViewById(R.id. bar );
       
bar2 = (ProgressBar) findViewById(R.id. bar2 );
       
//thread to process
       
new Thread()
        {
       
public void run()
        {
       
while ( status < 100)
        {
       
status = doWork();
       
mHandler .sendEmptyMessage(0x111);
        }
        }
        }.start();
    }
   
public int doWork()
    {
   
data [ hasData ++] = ( int ) (Math.random() * 100);
   
try
    {
    Thread.sleep(100);
    }
   
catch (InterruptedException e)
    {
    e.printStackTrace();
    }
   
return hasData ;
    }
}
main.xml代码:
< 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:orientation = "vertical"
>
< LinearLayout
   
android:orientation = "horizontal"
   
android:layout_width = "match_parent"
   
android:layout_height = "wrap_content" >
< ProgressBar
   
android:layout_width = "wrap_content"
   
android:layout_height = "wrap_content"
   
style = "@android:style/Widget.ProgressBar.Large" />
< ProgressBar
   
android:layout_width = "wrap_content"
   
android:layout_height = "wrap_content" />
< ProgressBar
   
android:layout_width = "wrap_content"
   
android:layout_height = "wrap_content"
   
style = "@android:style/Widget.ProgressBar.Small" />
</ LinearLayout >
< TextView
       
android:layout_width = "wrap_content"
       
android:layout_height = "wrap_content"
       
android:text = "Progress of completion." />
< ProgressBar
   
android:id = "@+id/bar"
   
android:layout_width = "match_parent"
   
android:layout_height = "wrap_content"
   
android:max = "100"
   
style = "@android:style/Widget.ProgressBar.Horizontal" />
< ProgressBar
   
android:id = "@+id/bar2"
   
android:layout_width = "match_parent"
   
android:layout_height = "wrap_content"
   
android:max = "100"
   
android:progressDrawable = "@drawable/my_bar"
   
style = "@android:style/Widget.ProgressBar.Horizontal" />

</ LinearLayout >
my_bar.xml代码;
<? xml version = "1.0" encoding = "utf-8" ?>
< layer-list xmlns:android = "http://schemas.android.com/apk/res/android" >
   
< item android:id = "@android:id/background"
       
android:drawable = "@drawable/no" />
   
< item android:id = "@android:id/progress"
       
android:drawable = "@drawable/ok" />
</ layer-list >
还有一种进度条,可以直接在标题上显示进度,这种进度条甚至不需要ProgressBar,它直接由Activity启用,使用步骤如下:
1.调用Activity的requestWindowFeature()方法
2.setProgressBarVisibility(boolean)或者setProgressBarIndeterminateVisibility(boolean)方法控制显示和隐藏
示例代码如下:
public class MainActivity extends ActionBarActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super .onCreate(savedInstanceState);
       
//progress bar on title
        requestWindowFeature(Window.
FEATURE_PROGRESS );
        requestWindowFeature(Window.
FEATURE_INDETERMINATE_PROGRESS );
        setContentView(R.layout.
activity_main );
        Button bn1 = (Button) findViewById(R.id.
bn1 );
        Button bn2 = (Button) findViewById(R.id.
bn2 );
        bn1.setOnClickListener(
new OnClickListener()
        {
       
@Override
       
public void onClick(View source)
        {
        setProgressBarIndeterminateVisibility(
true );
        setProgressBarVisibility(
true );
        setProgress(4500);
        }
        });

        bn2.setOnClickListener(
new OnClickListener()
        {
       
@Override
       
public void onClick(View source)
        {
        setProgressBarIndeterminateVisibility(
false );
        setProgressBarVisibility(
false );
        }
        });
    }
}
2.6.2 SeekBar的功能和用法 (拖动条)
SeekBar继承了ProgressBar,故ProgressBar所支持的XML属性和方法适用于SeekBar
SeekBar允许用户改变拖动条的滑块外观,改变滑块外观通过如下属性来指定:
android:thumb 指定一个Drawable对象,作为自定义滑块

2.6.3 RatingBar的功能和用法( 星级评分条
星级评分条与拖动条有相同的父类:AbsSeekBar
唉3是android:isIndicator
设置该星级评分条是否允许用户改变(true为不允许修改)
android:numStars
共有多少星级
android:rating
默认评分星级
android:stepSize
每次需要改变多少个星级
为了让程序响应星级评分条的改变,程序可为其绑定一个OnRatingBarChangeListenter监听器
示例代码:
public class MainActivity extends ActionBarActivity {

RatingBar
ratingBar ;
ImageView
image ;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super .onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main );
       
ratingBar = (RatingBar) findViewById(R.id. rating );
       
image = (ImageView) findViewById(R.id. iamge );
       
ratingBar .setOnRatingBarChangeListener( new OnRatingBarChangeListener()
        {
       
@Override
       
public void onRatingChanged(RatingBar arg0, float rating, boolean fromUser)
        {
       
image .setAlpha(( int )(rating * 255/5));
        }
        });
    }
xml代码:
< 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:orientation = "vertical" >

   
< ImageView
       
android:id = "@+id/iamge"
       
android:layout_width = "match_parent"
       
android:layout_height = "240px"
       
android:src = "@drawable/lijiang"
       
/>
   
< RatingBar
       
android:id = "@+id/rating"
       
android:layout_width = "wrap_content"
       
android:layout_height = "wrap_content"
       
android:numStars = "5"
       
android:max = "255"
       
android:progress = "255"
       
android:stepSize = "0.5"
       
/>
</ LinearLayout >



















你可能感兴趣的:(Android界面编程之六 第五组UI组件:ProgressBar及其子类)