Android ProgressBar 控件详解

本文通过四个实例介绍了Android中ProgressBar的不同用法,包括带进度条的、转圈样式、对话框样式及标题上的转圈样式,展示了如何通过Java代码控制进度。

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


1,带有进度条的 ProgressBar

Java 代码:


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Request the progress bar to be shown in the title
    requestWindowFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.progressbar_1);
    
    //设置在 title 里的 ProgressBar 可见
    setProgressBarVisibility(true);
    
    final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
    
    // 为 title 中的 ProgressBar 设置进度
    setProgress(progressHorizontal.getProgress() * 100);
    
    // 为title中的ProgressBar设置二级进度
    setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);
    
    // 一级进度递增
    Button button = (Button) findViewById(R.id.increase);
    button.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            progressHorizontal.incrementProgressBy(1);
            // Title progress is in range 0..10000
            setProgress(100 * progressHorizontal.getProgress());//为title中的ProgressBar设置进度
        }
    });

    //一级进度递减
    button = (Button) findViewById(R.id.decrease);
    button.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            progressHorizontal.incrementProgressBy(-1);
            // Title progress is in range 0..10000
            setProgress(100 * progressHorizontal.getProgress());//为title中的ProgressBar设置进度
        }
    });

    button = (Button) findViewById(R.id.increase_secondary);//二级进度递增
    button.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            progressHorizontal.incrementSecondaryProgressBy(1);
            // Title progress is in range 0..10000
            setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
        }
    });

    button = (Button) findViewById(R.id.decrease_secondary);//二级进度递减
    button.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            progressHorizontal.incrementSecondaryProgressBy(-1);
            // Title progress is in range 0..10000
            setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
        }
    });
    
}

布局文件 :

<ProgressBar android:id="@+id/progress_horizontal"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="200dip"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="50"
    android:secondaryProgress="75" />


程序运行效果图:

 

2,转圈的样式的 ProgressBar

Java 代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Request for the progress bar to be shown in the title
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    
    setContentView(R.layout.progressbar_2);
    
    // Make sure the progress bar is visible
    setProgressBarVisibility(true);
}

布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    // 大样式
    <ProgressBar
        android:id="@+android:id/progress_large"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    // 默认
    <ProgressBar                                  
        android:id="@+android:id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    // 小样式
    <ProgressBar
        android:id="@+android:id/progress_small"   
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    // 小标题样式
    <ProgressBar                                   
        android:id="@+android:id/progress_small_title"
        style="?android:attr/progressBarStyleSmallTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

运行效果如下图所示:

3,Dialog 样式的 ProgressBar

Java 代码:

 private static final int DIALOG1_KEY = 0;
    private static final int DIALOG2_KEY = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.progressbar_3);

        Button button = (Button) findViewById(R.id.showIndeterminate);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG1_KEY);
            }
        });

        button = (Button) findViewById(R.id.showIndeterminateNoTitle);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG2_KEY);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG1_KEY: {
                ProgressDialog dialog = new ProgressDialog(this);
                dialog.setTitle("Indeterminate");
                dialog.setMessage("Please wait while loading...");
                dialog.setIndeterminate(true);//设置转圈的效果
                dialog.setCancelable(true);
                return dialog;
            }
            case DIALOG2_KEY: {
                ProgressDialog dialog = new ProgressDialog(this);
                dialog.setMessage("Please wait while loading...");
                dialog.setIndeterminate(true);//设置转圈的效果
                dialog.setCancelable(true);
                return dialog;
            }
        }
        return null;
    }

程序运行效果如下所示:


4,在 title 上面的转圈的 ProgressBar

private boolean mToggleIndeterminate = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Request progress bar 
    requestWindowFeature(Window.FEATURE_PROGRESS);
    setContentView(R.layout.progressbar_4);
    
    // 是否可见
    setProgressBarIndeterminateVisibility(mToggleIndeterminate);
    
    Button button = (Button) findViewById(R.id.toggle);
    button.setOnClickListener(new Button.OnClickListener() {
        public void onClick(View v) {
            // 如果是false,设成true,如果是true设成false.
            mToggleIndeterminate = !mToggleIndeterminate;
            // 是否可见
            setProgressBarIndeterminateVisibility(mToggleIndeterminate);
        }
    });
}

程序运行效果如下图所示:


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chiclaim

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值