我一直在寻找关于如何使用AppCompat将不确定的水平进度条放置在操作栏下面的答案。我可以让水平进度条出现,但它在动作栏的顶部。我希望它在/在动作栏下面,有点像gmail是如何做到的(除了没有拉力刷新)。
我使用了以下代码来显示进度条:
supportRequestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.main_activity);
setSupportProgressBarIndeterminate(Boolean.TRUE);
setSupportProgressBarVisibility(true);
但这会将水平进度条放置在操作栏的顶部。有人知道如何将进度条放在动作栏下面吗?
发布于 2014-04-10 23:11:27
最近,我遇到了一个类似的问题,并通过创建自己的进度条,然后通过操作内容视图的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);
}
});
希望对你来说是有意义的。祝你好运!
https://stackoverflow.com/questions/21592573
复制相似问题