2012-04-10 70 views
3

我使用Actionbarsherlock爲我的應用程序,我喜歡在我的代碼中切換操作欄覆蓋模式。 我有2個片段(其中一個是Mapview,我想要一個半透明的Actionbar)另一個是ListFragment,其中需要一個堅實的Actionbar。Actionbarsherlock開關操作欄覆蓋模式編程

我tryed

requestWindowFeature((int) Window.FEATURE_ACTION_BAR & ~Window.FEATURE_ACTION_BAR_OVERLAY); 

問題,這是添加內容之前請求功能只適用。

我使用此樣式以實現透明動作條

<style name="TransparentActionbar" parent="@style/Theme.Sherlock" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item name="windowActionBarOverlay">true</item> 
    <item name="windowActionModeOverlay">true</item> 
    <item name="abBackground">#96000000</item> 
    <item name="abDivider">@null</item> 
</style> 

是有辦法來設置

<item name="windowActionBarOverlay">true</item> 
<item name="windowActionModeOverlay">true</item> 

到假活性/片段內?

回答

3

編輯:可惜它不適用於ActionBarsherlock和兼容包中的ListFragment。 出於某種原因,上限保證金被添加到底部保證金中。在LayoutListener內左右邊距工作正常。

找到此問題的解決方案在Android Developer Example

// Attach a GlobalLayoutListener so that we get a callback when the layout 
// has finished drawing. This is necessary so that we can apply top-margin 
// to the ListView in order to dodge the ActionBar. Ordinarily, that's not 
// necessary, but we've set the ActionBar to "overlay" mode using our theme, 
// so the layout does not account for the action bar position on its own. 
ViewTreeObserver observer = getListView().getViewTreeObserver(); 
observer.addOnGlobalLayoutListener(layoutListener); 

// Because the fragment doesn't have a reliable callback to notify us when 
// the activity's layout is completely drawn, this OnGlobalLayoutListener provides 
// the necessary callback so we can add top-margin to the ListView in order to dodge 
// the ActionBar. Which is necessary because the ActionBar is in overlay mode, meaning 
// that it will ordinarily sit on top of the activity layout as a top layer and 
// the ActionBar height can vary. Specifically, when on a small/normal size screen, 
// the action bar tabs appear in a second row, making the action bar twice as tall. 
ViewTreeObserver.OnGlobalLayoutListener layoutListener = new ViewTreeObserver.OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
     int barHeight = getActivity().getActionBar().getHeight(); 
     ListView listView = getListView(); 
     FrameLayout.LayoutParams params = (LayoutParams) listView.getLayoutParams(); 
     // The list view top-margin should always match the action bar height 
     if (params.topMargin != barHeight) { 
      params.topMargin = barHeight; 
      listView.setLayoutParams(params); 
     } 
     // The action bar doesn't update its height when hidden, so make top-margin zero 
     if (!getActivity().getActionBar().isShowing()) { 
      params.topMargin = 0; 
      listView.setLayoutParams(params); 
     } 
    } 
}; 
+0

謝謝你的男人。你已經解決了我的問題。 – tasomaniac 2013-05-28 10:39:50

+1

偉大的發現,正是我所需要的。對於那些閱讀還有一件事:在示例中,他們還從'onDestroyView()'中的'ViewTreeObserver'中移除了'OnGlobalLayoutListener'。像這樣:'getListView()。getViewTreeObserver()。removeGlobalOnLayoutListener(mLayoutListener);'可能很重要。 – 2014-11-08 00:51:34