【发布时间】: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