ProgressBar(进度条)
ProgressBar是进度条组件,通常用于向用户展示某个耗时操作完成的进度,而不让用户感觉是程序失去了响应,从而更好的提升用户界面的友好性。
1.指定ProgressBar显示风格
修改style属性即可修改显示风格
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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"/>
<ProgressBar
android:id="@+id/bar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
style="@android:style/Widget.ProgressBar.Inverse"/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="任务完成的进度"/>
<!-- 定义一个水平进度条 -->
<ProgressBar
android:id="@+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
style="@android:style/Widget.ProgressBar.Horizontal"/>
<!-- 定义一个水平进度条,并改变轨道外观 -->
</LinearLayout>
效果:
ProgressBar的分类:
注意还有一种显示在标题上的进度条
requestWindowFeature(Window.FEATURE_PROGRESS);
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_test1);
setProgressBarVisibility(true);
setProgressBarIndeterminate(true);
setProgress(600);
不过这种方法已过时
ProgressBar常用的XML属性
XML属性 | 说明 |
---|---|
android:max | 设置该进度条的最大值 |
android:progress | 设置该进度条的已完成进度值 |
android:progressDrawable | 设置该进度条的轨道对应的Drawable对象 |
android:indeterminate | 该属性设置为true,设置进度条不精确显示进度 |
android:indeterminateDrawable | 设置绘制不显示进度的进度条Drawable对象 |
android:indeterminateDuration | 设置不精确显示进度的持续时间 |
SeekBar(拖动条)
拖动条与进度条非常类似,只是进度条采用颜色填充来表明进度完成的程度,而拖动条则通过滑块的位置来标识并改变数值,如调节音量等。
android:thumb指定一个Drawable对象可以自定义滑块。
示例:
使用拖动滑块改变图片透明度
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/img5"/>
<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="255"
android:progress="255"/>
</LinearLayout>
public class Test1Activity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar);
final ImageView image = (ImageView)findViewById(R.id.img1);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
image.setImageAlpha(i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}
效果:
RatingBar(星级评分条)
与SeekBar类似,RatingBar通过星星来表示进度。
XML属性 | 说明 |
---|---|
android:isIndicator | 设置该星级评分条是否允许用户改变(true)为不允许 |
android:numStars | 设置该星级评分条总共有多少个星级 |
android:rating | 设置默认的星级 |
android:stepSize | 每次需要改变多少星级(步进) |
示例:
通过星级改变图片的透明度
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/img1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/img5"/>
<RatingBar
android:id="@+id/ratingbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:max="255"
android:progress="255"
android:stepSize="0.5"/>
</LinearLayout>
注意RatingBar控件的android:layout_width不要设置为match_parent,否则设置的最大星级无效,星级会占满一行。
public class Test1Activity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
RatingBar rat = (RatingBar)findViewById(R.id.ratingbar);
final ImageView image = (ImageView)findViewById(R.id.img1);
rat.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
image.setImageAlpha((int) v * 255 / 5);
}
});
}
}
效果: