如何从底部导航中删除标题

时间:2018-12-17 13:55:49

标签: android android-layout android-fragments android-intent

我调查了stackoverflow,找到了一种解决方法,该方法可以从默认的bottom navigation.中删除动画

现在我需要从底部导航中删除标题。我在底部导航xml文件的标题中设置了空字符串,但它不会更改图像的位置。 我正在使用v27

删除文字后,我需要将图像放在bottom navigation.中。

这是我的代码。

 //OnCreate 

        BottomNavigationViewHelper.removeShiftMode(bottomNavigationView);

MainActivity内部的静态类

public static class BottomNavigationViewHelper {
    @SuppressLint("RestrictedApi")
    public 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);
                //noinspection RestrictedApi
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BottomNav", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BottomNav", "Unable to change value of shift mode", e);
        }
    }
}

3 个答案:

答案 0 :(得分:1)

您可以尝试将app:labelVisibilityMode添加到“未标记”

 <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:labelVisibilityMode="unlabeled"/>

答案 1 :(得分:1)

我使用以下代码的变体来自定义BottomNavigationView的标签,这些标签本质上是TextViews:

private void removeBottomNavigationLabels(BottomNavigationView bottomNavigationView) {
    for (int i = 0; i < bottomNavigationView.getChildCount(); i++) {
        View item = bottomNavigationView.getChildAt(i);

        if (item instanceof BottomNavigationMenuView) {
            BottomNavigationMenuView menu = (BottomNavigationMenuView) item;

            for (int j = 0; j < menu.getChildCount(); j++) {
                View menuItem = menu.getChildAt(j);

                View small = menuItem.findViewById(android.support.design.R.id.smallLabel);
                if (small instanceof TextView) {
                    ((TextView) small).setVisibility(View.GONE);
                }
                View large = menuItem.findViewById(android.support.design.R.id.largeLabel);
                if (large instanceof TextView) {
                    ((TextView) large).setVisibility(View.GONE);
                }
            }
        }
    }

    BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
    for (int i = 0; i < menuView.getChildCount(); i++) {
        final View iconView = menuView.getChildAt(i).findViewById(android.support.design.R.id.icon);
        iconView.setPadding(0, 40, 0, 0);
        ViewGroup.LayoutParams layoutParams = iconView.getLayoutParams();
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        layoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, displayMetrics);
        layoutParams.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, displayMetrics);
        iconView.setLayoutParams(layoutParams);
    }
}

您可以这样称呼它:

removeBottomNavigationLabels(yourBottomNavigationView); 

您也可以尝试类似的方法来更改TextView的可见性,填充或高度。

答案 2 :(得分:-2)

尝试使用以下属性:

showUnselectedLabels: false
showSelectedLabels: false