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

use*_*843 8 android android-appcompat android-actionbar android-actionbar-compat

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

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

supportRequestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main_activity);
setSupportProgressBarIndeterminate(Boolean.TRUE);
setSupportProgressBarVisibility(true);
Run Code Online (Sandbox Code Playgroud)

但是这会将水平进度条放在操作栏的顶部.任何人都知道如何将进度条放在操作栏下面?

zol*_*ish 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);
Run Code Online (Sandbox Code Playgroud)

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

final ViewGroup decor = (ViewGroup) getWindow().getDecorView();
decor.addView(mLoadingProgressBar);
Run Code Online (Sandbox Code Playgroud)

并且为了使它到达正确的位置,我使用一个ViewTreeObserver侦听直到视图已经布局(也就是View.getTop()不是0).

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);
        }
    });
Run Code Online (Sandbox Code Playgroud)

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