Android控件之ProgressBar

本文介绍 Android 中 ProgressBar 控件的使用方法,包括不同样式展示及通过 Java 代码动态控制进度的实现方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

这里写图片描述

这里写图片描述

xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="默认样式"
        />
    <!-- 
    ProgressBar进度条控件,默认样式为圆形进度条(进度不确定)
    style:进度条样式
    max:最大进度值
    progress:当前进度值
    secondaryProgress:次要进度值
     -->
    <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 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleLarge"
        />

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="小号圆形样式"
        />
    <ProgressBar 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleSmall"
        />

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="水平方向长条形样式"
        />
    <ProgressBar
        android:id="@+id/progressBar" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:max="100"
        android:progress="30"
        android:secondaryProgress="50"
        />
    <Button 
        android:id="@+id/button_IncreaseProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="增加第一进度"
        />
    <Button 
        android:id="@+id/button_IncreaseSecondaryProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="增加第二进度"
        />
</LinearLayout>

xml文件效果
这里写图片描述

Java代码

package com.example.ui_progressbar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;


public class MainActivity extends Activity 
{
    private ProgressBar progressBar;
    private Button button_IncreaseProgress,button_IncreaseSecondaryProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.linearlayout);
        initView();
        progressBar.setMax(100);
        progressBar.setProgress(30);
        progressBar.setSecondaryProgress(50);
        ButtonListener buttonListener=new ButtonListener();
        button_IncreaseProgress.setOnClickListener(buttonListener);
        button_IncreaseSecondaryProgress.setOnClickListener(buttonListener);
    }

    private void initView() 
    {
        progressBar=(ProgressBar)findViewById(R.id.progressBar);
        button_IncreaseProgress=(Button)findViewById(R.id.button_IncreaseProgress);
        button_IncreaseSecondaryProgress=(Button)findViewById(R.id.button_IncreaseSecondaryProgress);
    }

    class ButtonListener implements OnClickListener
    {
        @Override
        public void onClick(View arg0) 
        {
            switch(arg0.getId())
            {
                case R.id.button_IncreaseProgress:
                {
                    //incrementProgressBy(int number):当前进度条增加number数值
                    progressBar.incrementProgressBy(20);
                    break;
                }
                case R.id.button_IncreaseSecondaryProgress:
                {
                    //incrementSecondaryProgressBy(int number):次要进度条增加number数值
                    progressBar.incrementSecondaryProgressBy(40);
                    break;
                }
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值