3

当我尝试更改我的 indeteminateDrawable 进度条时(我必须更改我的进度条的drawable。我尝试了无效,requestLayout 和其他。我不知道如何解决它。谢谢............ ..

这里代码:

progressBar.setIndeterminateDrawable(getResources().getDrawable(R.drawable.‌​progress_indeterminate_horizontal_didnt_like));

progressBar.setIndeterminate(true); 

是的。它在 XML 中可见。

 <ProgressBar
        android:id="@+id/progress"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="true"
         android:indeterminateDrawable="@drawable/progress_indeterminate_horizontal_to_review" />

  private void showProgressByCategory(String category) {
        progressBar = (ProgressBar) findViewById(R.id.progress);


        switch (CATEGORY.getPosition(category)) {
        case CATEGORY.DIDNT_LIKE_POS:
            // Didnt like
            progressBar.setIndeterminateDrawable(getResources().getDrawable(
                    R.drawable.progress_indeterminate_horizontal_didnt_like));

            break;
        case CATEGORY.TO_SEE_POS:
            // To See
            progressBar.setIndeterminateDrawable(getResources().getDrawable(
                    R.drawable.progress_indeterminate_horizontal_to_see));
            break;
        case CATEGORY.SAW_AND_LIKED_POS:
            // Saw And Liked
            progressBar.setIndeterminateDrawable(getResources().getDrawable(
                    R.drawable.progress_indeterminate_horizontal_saw_liked));
            break;
        case CATEGORY.TO_REVIEW_POS:
            // To Review
            progressBar.setIndeterminateDrawable(getResources().getDrawable(
                    R.drawable.progress_indeterminate_horizontal_to_review));

            break;
        }
        progressBar.setIndeterminate(true);
    }
4

4 回答 4

3

您必须先调用drawable.setBounds()固有大小才能setIndeterminateDrawable()解决

于 2016-07-27T07:21:06.373 回答
3

如果你的drawable是一个动画(drawable with animation-list),你应该把你的drawable称为AnimationDrwablesetIndeterminateDrawable为您的可绘制对象设置并调用 start。

AnimationDrawable drawable = getResources().getDrawable(R.drawable.your_custom_drawable);
    Rect bounds = progressBar.getIndeterminateDrawable().getBounds(); // get bounds from current drawable
    progressBar.setIndeterminateDrawable(drawable);
    drawable.start();
    progressBar.getIndeterminateDrawable().setBounds(bounds); //thanks ceabie
于 2017-07-25T16:11:18.223 回答
0

每当我想更改 Drawable 时,我通过创建一个新的 ProgressBar 来解决同样的问题。

笔记:

  1. 隐藏之前的进度条。
  2. 创建一个新的并显示它。

代码:

public class MyProgressDialog extends ProgressDialog {
    public MyProgressDialog(Context context) {
        super(context);
    }

    public ResultType resultType;

    public enum ResultType {
        eNone,
        eSuccess,
        eFail;
    }

    public void hide(String message, long completionIndicatorInterval) {
        hide();

        if ( (completionIndicatorInterval > 0) &&
                (resultType != ResultType.eNone)) {
            final MyProgressDialog completedDialog = new MyProgressDialog(getContext());
            completedDialog.setCancelable(false);
            completedDialog.setMessage(message);

            if (resultType == ResultType.eSuccess) {
                Drawable drawable = getContext().getResources().getDrawable(R.drawable.check);
                completedDialog.setIndeterminateDrawable(drawable);
                completedDialog.setIndeterminate(true);
            } else {
                Drawable drawable = getContext().getResources().getDrawable(R.drawable.close_image);
                completedDialog.setIndeterminateDrawable(drawable);
                completedDialog.setIndeterminate(true);
            }

            completedDialog.show();

            Handler handler = new Handler();
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    hide();
                    completedDialog.hide();
                }
            };
            handler.postDelayed(runnable, completionIndicatorInterval);
        }
    }
}
于 2015-04-02T07:27:55.603 回答
0

调用以下命令:

progressBar.setIndeterminateDrawable(yourDrawable)
progressBar.requestLayout();
于 2015-11-25T13:11:52.347 回答