2015-03-27 12 views
45

Ich habe die Dokumentation gesucht, aber nur gefunden: Link. Welches wird verwendet, um die Bar transluzent zu machen. Was ich versuche zu tun, um die Statusleiste macht vollständig transparent (wie im Bild unten) und macht es rückwärts kompatibel für APK < 19: enter image description hereAndroid Vollständig transparente Statusleiste?

Mein styles.xml:

<resources xmlns:tools="http://schemas.android.com/tools"> 
 

 
    <style name="AppTheme" parent="Theme.AppCompat.Light"> 
 
    <item name="android:actionBarStyle">@style/ThemeActionBar</item> 
 
    <item name="android:windowActionBarOverlay">true</item> 
 
    <!-- Support library compatibility --> 
 
    <item name="actionBarStyle">@style/ThemeActionBar</item> 
 
    <item name="windowActionBarOverlay">true</item> 
 
    </style> 
 

 
    <style name="ThemeActionBar" parent="Widget.AppCompat.Light.ActionBar.Solid"> 
 
    <item name="android:background"> @null </item> 
 
    <!-- Support library compatibility --> 
 
    <item name="background">@null</item> 
 
    <item name="android:displayOptions"> showHome | useLogo</item> 
 
    <item name="displayOptions">showHome|useLogo</item> 
 

 
    </style> 
 

 
</resources>

Was konnte ich tun:

enter image description here

Antwort

129

Alles, was Sie tun müssen, ist, diese Eigenschaften in Ihrem Thema gesetzt:

<item name="android:windowTranslucentStatus">true</item> 
<item name="android:windowTranslucentNavigation">true</item> 

Ihre Aktivität/Container Layout, das Sie wünschen eine transparente Statusleiste haben muss diese Eigenschaft festgelegt:

android:fitsSystemWindows="true" 

Es ist in der Regel nicht möglich, dies auf pre-kitkat sicher durchzuführen, sieht aus wie Sie es tun können but some strange code makes it so.

EDIT: Ich würde diese lib: https://github.com/jgilfelt/SystemBarTint für viele Vor-Lollipop Statusleiste Farbsteuerung empfehlen.

Nun nach reiflicher Überlegung habe ich gelernt, dass die Antwort völlig auf die Lichtdurchlässigkeit oder eine beliebige Farbe zu deaktivieren für Lutscher in der Statusleiste und Navigationsleiste platziert ist dieses Flag auf dem Fenster zu setzen:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
      Window w = getWindow(); // in Activity's onCreate() for instance 
      w.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS); 
     } 

Nein anderes Themen-ing ist notwendig, es so etwas wie diese produziert:

enter image description here

+0

Ich will nicht in Probleme oder Fehler laufen, wenn ich die Abwärtskompatibilität sage, meine ich, dass es einfach nicht transparent sein und wird die App nicht brechen. Entschuldige, dass ich es nicht beschrieben habe. –

+0

Oh, das ist alles, was passiert, wenn Sie diese 3 Eigenschaften oben setzen, wird die Statusleiste transparent sein> API 19, und normal auf

+5

Ich habe den Code verwendet und dann ein Bild als Hintergrund hinzugefügt und dann 'android hinzugefügt : fitsSystemWindows = "true" 'meiner activity_main.xml. Die Systemleisten sind durchscheinend und nicht transparent. –

2

diesen Code in ur xml verwendet, können Sie Zeit Bar in ur-Aktivität sehen:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true"> 
0

android:fitsSystemWindows="true" funktionieren nur auf v21. Wir können es im Design xml oder im übergeordneten Layout wie LinearLayout oder in CoordinateLayout setzen. Für unter V21 konnten wir dieses Flag nicht hinzufügen. Bitte erstellen Sie verschiedene Werte Ordner mit verschiedenen style.xml Datei nach Ihren Bedürfnissen.

2

Verwendung android: fitsSystemWindows = "false" in der Top-Layout

3

können Sie die externe Bibliothek verwenden StatusBarUtil:

in Ihre Modulebene build.gradle:

compile 'com.jaeger.statusbarutil:library:1.4.0' 

Dann können Sie verwenden die folgende util für eine Tätigkeit in der Statusleiste transparent zu machen:

StatusBarUtil.setTransparent(Activity activity) 

Beispiel:

transparent status bar on Lollipop and KitKat

0
<item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent</item> 
      <!--<item name="android:windowLightStatusBar" tools:targetApi="m">true</item>--> 

Dont verwenden windowLightStatusBar Verwendung anstelle statusBarColor = @android:color/transparent

0

Um Ihr Layout unter statusbar zu ziehen:

Werte/styles.xml

<item name="android:windowTranslucentStatus">true</item> 

Werte-v21/styles.xml

<item name="android:windowDrawsSystemBarBackgrounds">true</item> 
<item name="android:statusBarColor">@color/colorPrimaryDark</item> 

Verwendung CoordinatorLayout/DrawerLayout die bereits Pflege des fitsSystemWindows Parameter übernehmen oder ein eigenes Layout erstellen diese mögen:

public class FitsSystemWindowConstraintLayout extends ConstraintLayout { 

    private Drawable mStatusBarBackground; 
    private boolean mDrawStatusBarBackground; 

    private WindowInsetsCompat mLastInsets; 

    private Map<View, int[]> childsMargins = new HashMap<>(); 

    public FitsSystemWindowConstraintLayout(Context context) { 
     this(context, null); 
    } 

    public FitsSystemWindowConstraintLayout(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public FitsSystemWindowConstraintLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 

     if (ViewCompat.getFitsSystemWindows(this)) { 
      ViewCompat.setOnApplyWindowInsetsListener(this, new android.support.v4.view.OnApplyWindowInsetsListener() { 
       @Override 
       public WindowInsetsCompat onApplyWindowInsets(View view, WindowInsetsCompat insets) { 
        FitsSystemWindowConstraintLayout layout = (FitsSystemWindowConstraintLayout) view; 
        layout.setChildInsets(insets, insets.getSystemWindowInsetTop() > 0); 
        return insets.consumeSystemWindowInsets(); 
       } 
      }); 
      setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE 
        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); 
      TypedArray typedArray = context.obtainStyledAttributes(new int[]{android.R.attr.colorPrimaryDark}); 
      try { 
       mStatusBarBackground = typedArray.getDrawable(0); 
      } finally { 
       typedArray.recycle(); 
      } 
     } else { 
      mStatusBarBackground = null; 
     } 
    } 

    public void setChildInsets(WindowInsetsCompat insets, boolean draw) { 
     mLastInsets = insets; 
     mDrawStatusBarBackground = draw; 
     setWillNotDraw(!draw && getBackground() == null); 

     for (int i = 0; i < getChildCount(); i++) { 
      View child = getChildAt(i); 
      if (child.getVisibility() != GONE) { 
       if (ViewCompat.getFitsSystemWindows(this)) { 
        ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) child.getLayoutParams(); 

        if (ViewCompat.getFitsSystemWindows(child)) { 
         ViewCompat.dispatchApplyWindowInsets(child, insets); 
        } else { 
         int[] childMargins = childsMargins.get(child); 
         if (childMargins == null) { 
          childMargins = new int[]{layoutParams.leftMargin, layoutParams.topMargin, layoutParams.rightMargin, layoutParams.bottomMargin}; 
          childsMargins.put(child, childMargins); 
         } 
         if (layoutParams.leftToLeft == LayoutParams.PARENT_ID) { 
          layoutParams.leftMargin = childMargins[0] + insets.getSystemWindowInsetLeft(); 
         } 
         if (layoutParams.topToTop == LayoutParams.PARENT_ID) { 
          layoutParams.topMargin = childMargins[1] + insets.getSystemWindowInsetTop(); 
         } 
         if (layoutParams.rightToRight == LayoutParams.PARENT_ID) { 
          layoutParams.rightMargin = childMargins[2] + insets.getSystemWindowInsetRight(); 
         } 
         if (layoutParams.bottomToBottom == LayoutParams.PARENT_ID) { 
          layoutParams.bottomMargin = childMargins[3] + insets.getSystemWindowInsetBottom(); 
         } 
        } 
       } 
      } 
     } 

     requestLayout(); 
    } 

    public void setStatusBarBackground(Drawable bg) { 
     mStatusBarBackground = bg; 
     invalidate(); 
    } 

    public Drawable getStatusBarBackgroundDrawable() { 
     return mStatusBarBackground; 
    } 

    public void setStatusBarBackground(int resId) { 
     mStatusBarBackground = resId != 0 ? ContextCompat.getDrawable(getContext(), resId) : null; 
     invalidate(); 
    } 

    public void setStatusBarBackgroundColor(@ColorInt int color) { 
     mStatusBarBackground = new ColorDrawable(color); 
     invalidate(); 
    } 

    @Override 
    public void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     if (mDrawStatusBarBackground && mStatusBarBackground != null) { 
      int inset = mLastInsets != null ? mLastInsets.getSystemWindowInsetTop() : 0; 
      if (inset > 0) { 
       mStatusBarBackground.setBounds(0, 0, getWidth(), inset); 
       mStatusBarBackground.draw(canvas); 
      } 
     } 
    } 
} 

main_activity.xml

<FitsSystemWindowConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:fitsSystemWindows="true" 
     android:scaleType="centerCrop" 
     android:src="@drawable/toolbar_background" 
     app:layout_constraintBottom_toBottomOf="@id/toolbar" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="0dp" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@android:color/transparent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:gravity="center" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@id/toolbar"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="Content" 
      android:textSize="48sp" /> 
    </LinearLayout> 
</FitsSystemWindowConstraintLayout> 

Ergebnis:

Screenshot

1

Fügen Sie einfach diese Zeile Code zu Ihrer Haupt-Java-Datei:

getWindow().setFlags(
    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, 
    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS 
); 
Verwandte Themen