Created
October 25, 2018 13:06
-
-
Save MaTriXy/41847223da55ba1f087c582c7ef749c7 to your computer and use it in GitHub Desktop.
Nav Drawer Under Status Bar Tricks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:id="@+id/drawer_layout" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:fitsSystemWindows="true"> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<android.support.v7.widget.Toolbar | |
android:id="@+id/toolbar_actionbar" | |
android:layout_width="match_parent" | |
android:layout_height="?actionBarSize" | |
app:contentInsetStart="72dp" | |
android:elevation="4dp" | |
android:background="?colorPrimary" | |
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" | |
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" | |
tools:ignore="UnusedAttribute,NewApi" /> | |
<FrameLayout | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<View | |
android:id="@+id/main_shadow" | |
android:layout_width="match_parent" | |
android:layout_height="5dp" | |
android:background="?toolbar_shadow" /> | |
<FrameLayout | |
android:id="@+id/container" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</FrameLayout> | |
</LinearLayout> | |
<nexbit.icons.moonshine.views.ScrimInsetsFrameLayout | |
android:id="@+id/nav_drawer_frame" | |
android:layout_width="304dp" | |
android:layout_height="match_parent" | |
android:layout_gravity="start" | |
android:fitsSystemWindows="true" | |
app:insetForeground="#40000000"> | |
<fragment | |
android:id="@+id/navigation_drawer" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:name="nexbit.icons.moonshine.fragments.NavigationDrawerFragment" | |
android:tag="NAV_DRAWER" /> | |
</nexbit.icons.moonshine.views.ScrimInsetsFrameLayout> | |
</android.support.v4.widget.DrawerLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="ScrimInsetsView"> | |
<attr name="insetForeground" format="reference|color" /> | |
</declare-styleable> | |
<attr name="toolbar_shadow" format="reference" /> | |
</resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.content.res.TypedArray; | |
import android.graphics.Canvas; | |
import android.graphics.Rect; | |
import android.graphics.drawable.Drawable; | |
import android.support.v4.view.ViewCompat; | |
import android.util.AttributeSet; | |
import android.widget.FrameLayout; | |
/** | |
* A layout that draws something in the insets passed to {@link #fitSystemWindows(Rect)}, i.e. the area above UI chrome | |
* (status and navigation bars, overlay action bars). | |
*/ | |
public class ScrimInsetsFrameLayout extends FrameLayout { | |
private Drawable mInsetForeground; | |
private Rect mInsets; | |
private Rect mTempRect = new Rect(); | |
private OnInsetsCallback mOnInsetsCallback; | |
public ScrimInsetsFrameLayout(Context context) { | |
super(context); | |
init(context, null, 0); | |
} | |
public ScrimInsetsFrameLayout(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
init(context, attrs, 0); | |
} | |
public ScrimInsetsFrameLayout(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
init(context, attrs, defStyle); | |
} | |
private void init(Context context, AttributeSet attrs, int defStyle) { | |
final TypedArray a = context.obtainStyledAttributes(attrs, | |
R.styleable.ScrimInsetsView, defStyle, 0); | |
if (a == null) { | |
return; | |
} | |
mInsetForeground = a.getDrawable(R.styleable.ScrimInsetsView_insetForeground); | |
a.recycle(); | |
setWillNotDraw(true); | |
} | |
@Override | |
protected boolean fitSystemWindows(Rect insets) { | |
mInsets = new Rect(insets); | |
setWillNotDraw(mInsetForeground == null); | |
ViewCompat.postInvalidateOnAnimation(this); | |
if (mOnInsetsCallback != null) { | |
mOnInsetsCallback.onInsetsChanged(insets); | |
} | |
return true; // consume insets | |
} | |
@Override | |
public void draw(Canvas canvas) { | |
super.draw(canvas); | |
int width = getWidth(); | |
int height = getHeight(); | |
if (mInsets != null && mInsetForeground != null) { | |
int sc = canvas.save(); | |
canvas.translate(getScrollX(), getScrollY()); | |
// Top | |
mTempRect.set(0, 0, width, mInsets.top); | |
mInsetForeground.setBounds(mTempRect); | |
mInsetForeground.draw(canvas); | |
// Bottom | |
mTempRect.set(0, height - mInsets.bottom, width, height); | |
mInsetForeground.setBounds(mTempRect); | |
mInsetForeground.draw(canvas); | |
// Left | |
mTempRect.set(0, mInsets.top, mInsets.left, height - mInsets.bottom); | |
mInsetForeground.setBounds(mTempRect); | |
mInsetForeground.draw(canvas); | |
// Right | |
mTempRect.set(width - mInsets.right, mInsets.top, width, height - mInsets.bottom); | |
mInsetForeground.setBounds(mTempRect); | |
mInsetForeground.draw(canvas); | |
canvas.restoreToCount(sc); | |
} | |
} | |
@Override | |
protected void onAttachedToWindow() { | |
super.onAttachedToWindow(); | |
if (mInsetForeground != null) { | |
mInsetForeground.setCallback(this); | |
} | |
} | |
@Override | |
protected void onDetachedFromWindow() { | |
super.onDetachedFromWindow(); | |
if (mInsetForeground != null) { | |
mInsetForeground.setCallback(null); | |
} | |
} | |
/** | |
* Allows the calling container to specify a callback for custom processing when insets change (i.e. when | |
* {@link #fitSystemWindows(Rect)} is called. This is useful for setting padding on UI elements based on | |
* UI chrome insets (e.g. a Google Map or a ListView). When using with ListView or GridView, remember to set | |
* clipToPadding to false. | |
*/ | |
public void setOnInsetsCallback(OnInsetsCallback onInsetsCallback) { | |
mOnInsetsCallback = onInsetsCallback; | |
} | |
public static interface OnInsetsCallback { | |
public void onInsetsChanged(Rect insets); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- PUT IN: /res/values/styles.xml --> | |
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> | |
<item name="colorPrimary">#3F51B5</item> | |
<item name="colorPrimaryDark">#303F9F</item> | |
<item name="colorAccent">#E91E63</item> | |
<item name="toolbar_shadow">@drawable/toolbar_shadow</item> | |
</style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- PUT IN: /res/values-v21/styles.xml, NOT styles2.xml --> | |
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> | |
<item name="colorPrimary">#3F51B5</item> | |
<item name="colorPrimaryDark">#303F9F</item> | |
<item name="colorAccent">#E91E63</item> | |
<!-- Elevation will be used instead on API 21+ --> | |
<item name="toolbar_shadow">@null</item> | |
</style> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<shape xmlns:android="http://schemas.android.com/apk/res/android" | |
android:shape="rectangle"> | |
<gradient | |
android:angle="90" | |
android:endColor="#34000000" | |
android:startColor="@android:color/transparent" /> | |
</shape> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment