14

谷歌發佈新的支持庫V25與BottomNavigationView刪除BottomNavigationView標籤

enter image description here

是有什麼辦法可以刪除項目的標籤?

+0

你嘗試從菜單中刪除'標題'的嗎? –

+3

刪除標題後,圖標下方會出現額外的填充。添加'layout_marginBottom =「 - 16dp」'將刪除此填充,但將使所有視圖更小。 – dzikovskyy

+0

您可以不設置邊距,而是設置自定義高度並在頂部添加一些額外的填充。這樣你可以將圖標居中。 – Bolling

回答

14

不幸的是,這個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

+0

它將在低於21的apis上工作嗎? –

6

無反射方法:

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()); 
    } 
} 
+1

如果我想刪除圖標而不是文本 –

+1

如何使用此代碼? –

+0

如何在默認的BottomNavigation代碼已存在的主要活動中調用此方法? @NikolaDespotoski – TiagoIB

2

這是一個臨時修復。只需添加:app:itemTextColor="@android:color/transparent"無論背景顏色是什麼,都會顯示禁用。它確實使圖標看起來更高。

17

想要這種風格嗎?

如果是的話,我建議你去嘗試BottomNavigationViewEx

+1

底部菜單的最佳圖書館。謝謝..它的工作良好。 –

0

我建議你自己作爲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); 
     } 
    } 
} 
2
  1. 設置機器人:標題= 「」;在菜單/ abc.xml

  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); 
     } 
    } 
} 

`

  • 然後在主活性,添加這些行 mBottomNav =(BottomNavigationView)findViewById(R.id.navigation); BottomNavigationViewHelper。disableShiftMode(mBottomNav);