使用AppCompat不确定水平ProgressBar以下ActionBar?

时间:2014-02-06 01:59:58

标签: android android-actionbar android-actionbar-compat android-appcompat

我一直在寻找有关如何使用AppCompat将不确定的水平进度条放在操作栏下方的答案。我可以显示水平进度条,但它位于操作栏的顶部。我希望它在动作栏的下方/下方,就像gmail一样(除非没有刷新)。

我使用以下代码显示进度条:

supportRequestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main_activity);
setSupportProgressBarIndeterminate(Boolean.TRUE);
setSupportProgressBarVisibility(true);

但这会将水平进度条放在操作栏的顶部。有人知道如何将进度条放在操作栏下面吗?

1 个答案:

答案 0 :(得分:6)

我最近遇到了类似的问题并通过创建我自己的进度条然后通过操纵内容视图的getTop()来对齐它来解决它。

首先创建进度条。

final LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, 20); //Use dp resources


mLoadingProgressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
mLoadingProgressBar.setIndeterminate(true);
mLoadingProgressBar.setLayoutParams(lp);

将其添加到窗口(装饰视图)

final ViewGroup decor = (ViewGroup) getWindow().getDecorView();
decor.addView(mLoadingProgressBar);

为了使它到达正确的位置,我使用一个监听视图的ViewTreeObserver(也就是View.getTop()不是0)。 < / p>

final ViewTreeObserver vto = decor.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        final View content = getView(android.R.id.content);

        @Override
        public void onGlobalLayout() {
            int top = content.getTop();

            //Dont do anything until getTop has a value above 0.
            if (top == 0)
                return;

            //I use ActionBar Overlay in some Activities, 
            //in those cases it's size has to be accounted for
            //Otherwise the progressbar will show up at the top of it
            //rather than under. 

            if (getSherlock().hasFeature((int) Window.FEATURE_ACTION_BAR_OVERLAY)) {
                top += getSupportActionBar().getHeight();
            }

            //Remove the listener, we dont need it anymore.
            Utils.removeOnGlobalLayoutListener(decor, this);

            //View.setY() if you're using API 11+, 
            //I use NineOldAndroids to support older 
            ViewHelper.setY(mLoadingProgressBar, top);
        }
    });

希望对你有意义。祝你好运!