安卓UI控件-ProgressBar及其子类

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);
            }
        });
    }
}

效果:

这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值