如何在确定模式下使用 Android ProgressBar?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2967337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:05:41  来源:igfitidea点击:

How do I use Android ProgressBar in determinate mode?

androidprogress-barmedia-player

提问by mtmurdock

I am writing a media player and i would like to have a progress bar showing the progress of the song. I found the ProgressBar class, but all i can get on the screen is a circular spinning icon. what im looking for is an actual bar.

我正在编写一个媒体播放器,我想要一个显示歌曲进度的进度条。我找到了 ProgressBar 类,但我只能在屏幕上看到一个圆形的旋转图标。我要找的是一个真正的酒吧。

How do i change the style of the ProgressBar to be a bar (not a circle) and how would i use it with MediaPlayer?

如何将 ProgressBar 的样式更改为条形(而不是圆形),以及如何将其与 MediaPlayer 一起使用?

thanks

谢谢

回答by Jorgesys

use the style ?android:attr/progressBarStyleHorizontal

使用样式 ?android:attr/progressBarStyleHorizontal

for example:

例如:

  <ProgressBar android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"

and this is an example with MediaPlayer:

这是 MediaPlayer 的示例:

package com.playerpgbar;

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

public class Player extends Activity implements Runnable, OnClickListener {

    private TextView status;
    private ProgressBar progressBar;
    private Button startMedia;
    private Button stop;
    private MediaPlayer mp;      

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        status = (TextView) findViewById(R.id.status);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        startMedia = (Button) findViewById(R.id.startMedia);
        stop = (Button) findViewById(R.id.stop);

        startMedia.setOnClickListener(this);
        stop.setOnClickListener(this);                
    }        

    @Override
    public void onClick(View v) {
        if (v.equals(startMedia)) {
            if (mp != null && mp.isPlaying()) return;
            mp = MediaPlayer.create(Player.this, R.raw.exodus_piranha);
            mp.start();               
            status.setText(R.string.PlayingMedia);         
            progressBar.setVisibility(ProgressBar.VISIBLE);
            progressBar.setProgress(0);
            progressBar.setMax(mp.getDuration());
            new Thread(this).start();
        }

        if (v.equals(stop) && mp!=null) {
            mp.stop();
            mp = null;            
            status.setText(R.string.Stopped);
            progressBar.setVisibility(ProgressBar.GONE);
        }
    }

    @Override
    public void run() {
        int currentPosition= 0;
        int total = mp.getDuration();
        while (mp!=null && currentPosition<total) {
            try {
                Thread.sleep(1000);
                currentPosition= mp.getCurrentPosition();
            } catch (InterruptedException e) {
                return;
            } catch (Exception e) {
                return;
            }            
            progressBar.setProgress(CurrentPosition);
        }
    }
}

回答by nmr

Programmatically:

以编程方式:

progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleHorizontal);
progressBar.setIndeterminate(false);