【问题标题】:Indeterminate Horizontal ProgressBar BELOW ActionBar using AppCompat?使用AppCompat在ActionBar下方不确定水平ProgressBar?
【发布时间】:2014-03-02 18:55:30
【问题描述】:

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

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

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

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

【问题讨论】:

  • stackoverflow.com/questions/13934010/… 建议了一种解决方法,但这种解决方案几乎违背了尝试使用 AppCompat 的全部目的。任何人都可以使用 AppCompat 将进度条定位在操作栏下方吗?
  • 难以置信,这么常见的问题竟然没有简单的解决方案……

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


【解决方案1】:

我最近遇到了类似的问题,并通过创建自己的进度条然后通过操作内容视图的 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)。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多