【问题标题】:Remove BottomNavigationView labels移除 BottomNavigationView 标签
【发布时间】:2017-03-04 03:42:07
【问题描述】:

Google 发布了带有 BottomNavigationView 的新支持库 v25

有什么办法可以去掉物品标签吗?

【问题讨论】:

  • 您是否尝试从<item>s 菜单中删除titles?
  • 删除标题后,图标下方有额外的填充。添加layout_marginBottom="-16dp" 将删除此填充,但会使所有视图变小。
  • 您可以代替设置边距,设置自定义高度并在顶部添加一些额外的填充。这样,您可以将图标居中。
  • 我只是这样修复它:android:paddingTop="8dp" android:layout_marginBottom="-8dp" 这可以防止栏变小

标签: android android-support-library androiddesignsupport bottomnavigationview


【解决方案1】:

希望我来这里参加派对还不算太晚。

但从设计支持库 28.0.0-alpha1 开始 您可以使用该属性

app:labelVisibilityMode="unlabeled"

您也可以使用其他值“auto”、“labeled”和“selected”。

【讨论】:

  • 支持哪些库? v7 还是设计?
  • 设计支持库28.0.0
  • 你是在哪个班写的?
  • @Explorex 这是android的底部导航视图,我提到的属性是在所述视图的xml标签中使用的所述类的xml属性。你也可以在代码中使用它developer.android.com/reference/com/google/android/material/…
【解决方案2】:

你想要这种风格吗?

如果是这样,我建议你试试BottomNavigationViewEx

【讨论】:

  • 我不喜欢这个库。它会在单击时更改图标位置,并且无法在单击时固定图标位置。
【解决方案3】:

不幸的是,BottomNavigationView 的第一个版本有很多限制。现在您不能仅使用支持设计 API 来删除标题。所以要解决这个限制,而谷歌没有实现它,你可以做(​​使用反射):

1.在 bottom_navigation_menu.xml 文件中将标题设置为空。

2。扩展 BottomNavigationView:

    public class MyBottomNavigationView extends BottomNavigationView {

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

      private void centerMenuIcon() {
          BottomNavigationMenuView menuView = getBottomMenuView();

          if (menuView != null) {
              for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView menuItemView = (BottomNavigationItemView) menuView.getChildAt(i);

                AppCompatImageView icon = (AppCompatImageView) menuItemView.getChildAt(0);

                FrameLayout.LayoutParams params = (LayoutParams) icon.getLayoutParams();
                params.gravity = Gravity.CENTER;

                menuItemView.setShiftingMode(true);
              }
          }
      }

      private BottomNavigationMenuView getBottomMenuView() {
          Object menuView = null;
          try {
              Field field = BottomNavigationView.class.getDeclaredField("mMenuView");
              field.setAccessible(true);
              menuView = field.get(this);
          } catch (NoSuchFieldException | IllegalAccessException e) {
              e.printStackTrace();
          }

          return (BottomNavigationMenuView) menuView;
      }
    }

3.在 layout.xml 中添加这个 customView

有关更多详细信息,我已在 Github 上实现此功能

【讨论】:

  • 无需使用反射,您可以通过使用菜单项 ID 调用 findViewById() 来获取每个 BottomNavigationItemView(就像 @NikolaDespotoski 在 his answer 中所做的那样)。
  • 他们现在支持删除标签app:labelVisibilityMode="unlabeled"
【解决方案4】:

1.menu/abc.xml

中设置android:title="";

2. 创建下面使用反射的辅助类

import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.widget.AppCompatImageView;
import android.util.Log;
import android.view.Gravity;
import android.widget.FrameLayout;

import java.lang.reflect.Field;

public class BottomNavigationViewHelper {
    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);
                //noinspection RestrictedApi
                item.setShiftingMode(false);
                item.setPadding(0, 15, 0, 0);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked(item.getItemData().isChecked());
            }
        } 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);
        }
    }
} 

3.在您的主要活动中,添加以下行:

mBottomNav = (BottomNavigationView) findViewById(R.id.navigation);
BottomNavigationViewHelper.disableShiftMode(mBottomNav);

【讨论】:

    【解决方案5】:

    无反思的方法:

    private void removeTextLabel(@NonNull BottomNavigationView bottomNavigationView, @IdRes int menuItemId) {
        View view = bottomNavigationView.findViewById(menuItemId);
        if (view == null) return;
        if (view instanceof MenuView.ItemView) {
            ViewGroup viewGroup = (ViewGroup) view;
            int padding = 0;
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                View v = viewGroup.getChildAt(i);
                if (v instanceof ViewGroup) {
                    padding = v.getHeight();
                    viewGroup.removeViewAt(i);
                }
            }
            viewGroup.setPadding(view.getPaddingLeft(), (viewGroup.getPaddingTop() + padding) / 2, view.getPaddingRight(), view.getPaddingBottom());
        }
    }
    

    【讨论】:

    • 如果我想删除图标而不是文本呢
    • 如何在默认底部导航代码已经存在的主要活动中调用它? @NikolaDespotoski
    • @TiagoIB 只是将方法设为静态并将其移动到其他类。或者保持私有并使用指定的参数调用它。
    • 对不起,我怎么能在我的代码中插入它?prntscr.com/he03j7@NikolaDespotoski
    【解决方案6】:

    这是一个临时修复。只需添加:app:itemTextColor="@android:color/transparent" 无论背景颜色是什么,它都会显示为已禁用。它确实使图标看起来升高了。

    【讨论】:

      【解决方案7】:

      我想同时删除 shift animation 和标签,但这里没有一个解决方案适合我,所以这是我根据我在这里学到的一切构建的解决方案:

      public void removeLabels(@IdRes int... menuItemIds) {
          getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
              @Override public boolean onPreDraw() {
                  getViewTreeObserver().removeOnPreDrawListener(this);
      
                  // this only needs to be calculated once for an unchecked item, it'll be the same value for all items
                  ViewGroup uncheckedItem = findFirstUncheckedItem(menuItemIds);
                  View icon = uncheckedItem.getChildAt(0);
                  int iconTopMargin = ((LayoutParams) uncheckedItem.getChildAt(0).getLayoutParams()).topMargin;
                  int desiredTopMargin = (uncheckedItem.getHeight() - uncheckedItem.getChildAt(0).getHeight()) / 2;
                  int itemTopPadding = desiredTopMargin - iconTopMargin;
      
                  for (int id : menuItemIds) {
                      ViewGroup item = findViewById(id);
                      // remove the label
                      item.removeViewAt(1);
                      // and then center the icon
                      item.setPadding(item.getPaddingLeft(), itemTopPadding, item.getPaddingRight(),
                              item.getPaddingBottom());
                  }
      
                  return true;
              }
          });
      }
      
      @SuppressLint("RestrictedApi")
      private ViewGroup findFirstUncheckedItem(@IdRes int... menuItemIds) {
          BottomNavigationItemView item = findViewById(menuItemIds[0]);
          int i = 1;
          while (item.getItemData().isChecked()) {
              item = findViewById(menuItemIds[i++]);
          }
          return item;
      }
      

      只需将此方法添加到您的自定义 BottomNavigationView 并通过菜单项的 ID 调用它。

      【讨论】:

        【解决方案8】:

        我建议您自己将其实现为sanf0rd gave in his answer。但是AppCompatImageView 不适合我。我已将其更改为ImageView。并将getChildAt 更改为findViewById

        我还隐藏了未选中项目的所有标签。

        private void centerMenuIcon() {
            BottomNavigationMenuView menuView = getBottomMenuView();
            if (menuView != null) {
                for (int i = 0; i < menuView.getChildCount(); i++) {
                    BottomNavigationItemView menuItemView = (BottomNavigationItemView) menuView.getChildAt(i);
                    TextView smallText = (TextView) menuItemView.findViewById(R.id.smallLabel);
                    smallText.setVisibility(View.INVISIBLE);
                    //TextView largeText = (TextView) menuItemView.findViewById(R.id.largeLabel);
                    ImageView icon = (ImageView) menuItemView.findViewById(R.id.icon);
                    FrameLayout.LayoutParams params = (LayoutParams) icon.getLayoutParams();
                    params.gravity = Gravity.CENTER;
                    menuItemView.setShiftingMode(true);
                }
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-04-18
          • 1970-01-01
          • 2017-03-16
          • 1970-01-01
          • 1970-01-01
          • 2018-02-02
          • 2017-06-11
          相关资源
          最近更新 更多