2016-12-25 145 views
1

我正在閱讀material.io指南,我在「底部導航」部分看到此圖片,並試圖做到這一點,但結果並非我想要的。 What I get帶底部導航視圖的半透明導航

What I want

styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> 
     <item name="colorPrimary">@color/colorPrimary</item> 
     <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
     <item name="colorAccent">@color/colorAccent</item> 
     <item name="android:windowTranslucentNavigation" tools:targetApi="kitkat">true</item> 
     <item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item> 
</style> 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 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:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.amir_p.headphone.MainActivity"> 

    <android.support.design.widget.BottomNavigationView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:fitsSystemWindows="true" 
     app:itemBackground="@color/colorPrimary" 
     app:itemIconTint="@color/white" 
     app:itemTextColor="@color/white" 
     app:menu="@menu/navigation" /> 
</android.support.design.widget.CoordinatorLayout> 

如何我就與BottomNavigationView像材料指引一個半透明的導航欄?

回答

0

這就是我是如何解決這個問題:

MyBottomNavigation.java

public final class MyBottomNavigationView extends BottomNavigationView implements View.OnApplyWindowInsetsListener { 

private int height; 

public MyBottomNavigationView(Context context) { 
    super(context); 

    init(context); 
} 

public MyBottomNavigationView(Context context, AttributeSet attrs) { 
    super(context, attrs); 

    init(context); 
} 

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

    init(context); 
} 

private void init(final Context context) { 
    height = getDefaultHeight(context); 
    setMenuViewGravity(getMenuView(), Gravity.TOP); 
    setOnApplyWindowInsetsListener(this);   
} 

private int getDefaultHeight(Context context) { 
    int height; 

    TypedArray a = context.getTheme().obtainStyledAttributes(new int[] {android.R.attr.actionBarSize}); 

    try { 
     height = a.getDimensionPixelSize(0, 0); 
    } finally { 
     a.recycle(); 
    } 

    return height; 
} 

private View getMenuView() { 
    return getChildAt(0); 
} 

private void setMenuViewGravity(View menuView, int gravity) { 
    ((LayoutParams) menuView.getLayoutParams()).gravity = gravity; 
} 

private void setNavigationHeight(final int height) { 
    getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
     @Override 
     public boolean onPreDraw() { 
      getLayoutParams().height = height; 
      getViewTreeObserver().removeOnPreDrawListener(this); 
      return true; 
     } 
    }); 
} 

@Override 
public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) { 
    setNavigationHeight(height + insets.getSystemWindowInsetBottom()); 
    return insets.replaceSystemWindowInsets(insets.getSystemWindowInsetLeft(), 0, insets.getSystemWindowInsetRight(), 0); 
} 
} 

而且 activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    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" 
    tools:context="as.ways.iqey.main.MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/app_bar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:animateLayoutChanges="true"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 

</android.support.design.widget.AppBarLayout> 

<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fillViewport="true" 
    android:paddingBottom="56dp" 
    android:overScrollMode="never" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

</android.support.v4.widget.NestedScrollView> 

<as.ways.iqey.main.MyBottomNavigationView 
    android:id="@+id/navigation" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:fitsSystemWindows="true" 
    android:background="?android:attr/windowBackground" 
    app:theme="@style/AppTheme.BottomNavigationOverlay" 
    android:layout_gravity="bottom" 
    app:menu="@menu/navigation" /> 

</android.support.design.widget.CoordinatorLayout>