2012-04-10 9 views
3

Ich benutze Actionbarsherlock für meine App und ich möchte den Actionbar Overlay Modus in meinem Code ändern. Ich habe 2 Fragmente (eine ist eine Mapview, wo ich eine transluzente Actionbar möchte) die andere ist eine ListFragment, wo eine solide Actionbar benötigt wird.Actionbarsherlock Schalter Actionbar Overlay Modus programmatisch

i tryed

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

Problem dabei ist, dass Funktionen nur anfordernden funktioniert, bevor Inhalt hinzugefügt wird.

ich diesen Stil mit der transparenten ActionBar

<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> 

ist es eine Möglichkeit,

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

auf false in der Aktivität/Fragment einstellen zu erreichen?

Antwort

3

Bearbeiten: Leider funktioniert es nicht für ActionBarsherlock und eine ListFragment aus dem Kompatibilitätspaket. Oberer Rand wird aus irgendeinem Grund zum unteren Rand hinzugefügt. Linker und rechter Rand funktionieren im LayoutListener einwandfrei.

eine Lösung für dieses Problem gefunden in einem 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

Danke Mann. Du hast mein Problem gelöst. – tasomaniac

+1

Toller Fund, genau was ich brauchte. Für diejenigen, die lesen, gibt es noch eine Sache: Im Beispiel entfernen sie auch den 'OnGlobalLayoutListener' aus dem' ViewTreeObserver' in 'onDestroyView()'. Wie folgt: 'getListView(). GetViewTreeObserver(). RemoveGlobalOnLayoutListener (mLayoutListener);' Könnte wichtig sein. –

Verwandte Themen