8

我一直在尋找如何將不確定的水平進度條放置在使用AppCompat的操作欄下的解答。我可以顯示水平進度條,但它位於操作欄的頂部。我希望它在操作欄下面/下面,就像gmail做的那樣(除非沒有拉動刷新)。不確定的水平ProgressBar下面使用AppCompat的ActionBar?

我用下面的代碼有進度條顯示:

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

,但這個地方的水平進度條在操作欄的頂部。任何人都知道如何將進度條放置在操作欄下方?

+1

有一個解決方法建議:http://stackoverflow.com/questions/13934010/progressbar-under-action-bar/15073680#15073680但這種解決方案几乎打敗了嘗試使用AppCompat的整個目的。任何人都可以使用AppCompat將進度條放置在操作欄下方? – user2382843

+0

令人難以置信的是這樣一個常見的問題沒有簡單的解決方案... –

回答

6

我最近遇到了類似問題,並通過創建自己的進度條,然後通過操作內容視圖的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); 
     } 
    }); 

希望是有道理的你。祝你好運!

相關問題