如何在底部导航视图中只有一个图标(而不是动画)

时间:2018-03-01 06:54:59

标签: java android android-animation bottomnavigationview

移位模式已关闭。按下图标后,图标会向上移动。我不希望这种情况发生。我只想在按下图标时更改颜色。我想要中间的图标。

该课程已实施:

public class BottomNavigationViewHelper {

@SuppressLint("RestrictedApi")
public static void disableShiftMode(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);
            AppCompatImageView icon = (AppCompatImageView) item.getChildAt(0);
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) icon.getLayoutParams();
            params.gravity = Gravity.CENTER;
        }
    } catch (NoSuchFieldException e) {
        Log.e("BNVHelper", "Unable to get shift mode field", e);
    } catch (IllegalAccessException e) {
        Log.e("BNVHelper", "Unable to change value of shift mode", e);
    }
}
}

在已实施的课程中:

BottomNavigationViewHelper.disableShiftMode(bottomNavigation);

布局XML:

<android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        app:elevation="16dp"
        android:layout_weight="1"
        android:animateLayoutChanges="false"
        app:itemBackground="@color/Gray"
        app:itemIconTint="@drawable/nav_item_color_state"
        app:itemTextColor="@drawable/nav_item_color_state"
        app:menu="@menu/bottom_navigation_main" />

菜单 - &gt; bottom_navigation_main xml示例:

<item   android:id="@+id/star"
        android:enabled="true"
        android:icon="@drawable/ic_star"
        android:title=""
        app:showAsAction="never" />

0 个答案:

没有答案