【问题标题】:ProgressBar is displayed in the middle of ActionBarProgressBar 显示在 ActionBar 中间
【发布时间】:2016-01-09 19:35:39
【问题描述】:

我正在开发一个显示来自不同来源的新闻的应用程序。当点击新闻时,它会打开一个由WebView 组成的activity,以传递与新闻相关的网页。与WebView 一起,我实现了ProgressBar 来显示加载页面的进度。我不希望它占用内容的空间,所以我尝试在ActionBar 的底部实现它。但结果不是我例外,如下图所示。

我使用的代码如下:

// create new ProgressBar and style it
        final ProgressBar progressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);
        progressBar.setLayoutParams(new android.app.ActionBar.LayoutParams(android.app.ActionBar.LayoutParams.MATCH_PARENT, 24));

        // retrieve the top view of our application
        final FrameLayout decorView = (FrameLayout) getWindow().getDecorView();
        decorView.addView(progressBar);

        ViewTreeObserver observer = progressBar.getViewTreeObserver();
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                View contentView = decorView.findViewById(android.R.id.content);
                progressBar.setY(contentView.getY()-10);
                progressBar.setX(0);

                ViewTreeObserver observer = progressBar.getViewTreeObserver();
                observer.removeGlobalOnLayoutListener(this);
            }
        });

我指的是这个问题:ProgressBar under Action Bar

你能告诉我我在代码中做错了什么,或者其他方式来实现这一点。

【问题讨论】:

    标签: android android-actionbar android-progressbar


    【解决方案1】:

    使用 offsetTopAndBottom 改变进度条的偏移量。 见:ProgressBar

    【讨论】:

    • 我在这个表单中使用了progressBar.offsetTopAndBottom((int)contentView.getY()-10);,现在它没有显示任何内容。
    • 尝试更改参数尝试向下移动。我认为您需要为其添加一些价值。
    猜你喜欢
    • 2017-05-31
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-19
    • 2010-12-01
    • 1970-01-01
    相关资源
    最近更新 更多