2017-10-05 112 views
0

我正在研究Android的BottomNavigation。如何在CoordinatorLayout android中修复bottom_avigation菜单项的位置?

我为我的BottomNavigation创建了一个ViewBehavior类。我的BottomNavigation中有四个项目。我面临的问题是所选项目占用较大的空间。当我选择任何项目时,它会占用更多的位置,然后其他人会使用一些动画。但我需要修复这些物品的位置。每个

什么是可能的解决方案?

我BottomNavigationViewBehavior类是:

public class BottomNavigationViewBehavior extends CoordinatorLayout.Behavior<BottomNavigationView> { 

private int height; 

@Override 
public boolean onLayoutChild(CoordinatorLayout parent, BottomNavigationView child, int layoutDirection) { 
    height = child.getHeight(); 
    return super.onLayoutChild(parent, child, layoutDirection); 
} 

@Override 
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View directTargetChild, View target, int nestedScrollAxes) { 
    return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL; 
} 

@Override 
public void onNestedScroll(CoordinatorLayout coordinatorLayout, BottomNavigationView child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) { 
    if (dyConsumed > 0) { 
     slideDown(child); 
    } else if (dyConsumed < 0) { 
     slideUp(child); 
    } 
} 

private void slideUp(BottomNavigationView child) { 
    child.clearAnimation(); 
    child.animate().translationY(0).setDuration(200); 
} 

private void slideDown(BottomNavigationView child) { 
    child.clearAnimation(); 
    child.animate().translationY(height).setDuration(200); 
} 

} 

这里是我的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.support.v4.view.ViewPager 
    android:id="@+id/viewpager" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_above="@+id/bottomNavigation" 
    android:layout_alignParentTop="true"/> 

<android.support.design.widget.BottomNavigationView 
    android:id="@+id/bottomNavigation" 
    android:layout_width="match_parent" 
    android:layout_height="56dp" 
    android:layout_gravity="bottom" 
    android:background="@color/colorAccent" 
    app:menu="@menu/navigation" /> 

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

我现在的情况

Selected 4th item took more place then others

回答

1

使用这个类

class BottomNavigationViewHelper { 

static void removeShiftMode(BottomNavigationView view) { 
    BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0); 
    try { 
     Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode"); 
     shiftingMode.setAccessible(true); 
     shiftingMode.setBoolean(menuView, false); 
     shiftingMode.setAccessible(false); 
     for (int i = 0; i < menuView.getChildCount(); i++) { 
      BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i); 
      item.setShiftingMode(false); 
      // set once again checked value, so view will be updated 
      item.setChecked(item.getItemData().isChecked()); 
     } 
    } catch (NoSuchFieldException e) { 
     Log.e("ERROR NO SUCH FIELD", "Unable to get shift mode field"); 
    } catch (IllegalAccessException e) { 
     Log.e("ERROR ILLEGAL ALG", "Unable to change value of shift mode"); 
    } 
} 
} 

而且使用这个类是这样的:

BottomNavigationView bottomNavigationView =(BottomNavigationView)findViewById(R.id.bottomBar); 
BottomNavigationViewHelper.removeShiftMode(bottomNavigationView); 

原始answer

1

确定。最后,我从Here得到了我的答案。我只需要制作另一个类或方法作为BottomNavigationViewHelper。