Indeterminate Horizontal ProgressBar BELOW ActionBar using AppCompat?

假装没事ソ 提交于 2019-11-29 03:11:28

问题


I have been looking for answers on how to place the indeterminate horizontal progress bar below the action bar using AppCompat. I'm able to get the horizontal progress bar to appear, but it is at the top of the action bar. I want it under/below the action bar kind of like how gmail does it (except without the pull to refresh).

I used the following code to have the progress bar appear:

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

but this places the horizontal progress bar at the top of the action bar. Anyone know how to place the progress bar below the action bar?


回答1:


I faced a similar problem recently and solved it by creating my own progressbar and then aligning it by manipulating getTop() of the content view.

So first create your progressbar.

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);

Add it to the window (decor view)

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

And in order to get it to its correct position Im using a ViewTreeObserver that listens until the view has been laid out (aka the View.getTop() isnt 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);
        }
    });

Hope that makes sense for you. Good luck!



来源:https://stackoverflow.com/questions/21592573/indeterminate-horizontal-progressbar-below-actionbar-using-appcompat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!