我最近遇到了類似問題,並通過創建自己的進度條,然後通過操作內容視圖的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
,監聽,直到認爲已經奠定了它到達其正確的位置爲IM (又名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);
}
});
希望是有道理的你。祝你好運!
有一個解決方法建議:http://stackoverflow.com/questions/13934010/progressbar-under-action-bar/15073680#15073680但這種解決方案几乎打敗了嘗試使用AppCompat的整個目的。任何人都可以使用AppCompat將進度條放置在操作欄下方? – user2382843
令人難以置信的是這樣一個常見的問題沒有簡單的解決方案... –