0

我正在使用android支持设计小部件BottomNavigationView来制作我的底部导航项目。这是工作的结果: enter image description hereandroid-支持库BottomNavigationView不显示标题

它有2个问题,一是它不显示标题的项目下,第二个问题我想要的物品,以填补​​着,我不希望有导航上的自由空间,我只是希望他们填充空间并划分项目之间的空间。

这是我的菜单代码:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <item 
     android:id="@+id/action_item1" 
     android:icon="@drawable/phone" 
     app:showAsAction="ifRoom" 
     android:title="ارتباط" /> 

    <item 
     android:id="@+id/action_item2" 
     android:icon="@mipmap/ic_history" 
     app:showAsAction="ifRoom" 
     android:title="سوابق خرید" /> 

    <item 
     android:id="@+id/action_item3" 
     android:icon="@drawable/sabad" 
     app:showAsAction="ifRoom" 
     android:title="سبد خرید" /> 

    <item 
     android:id="@+id/action_item4" 
     android:icon="@drawable/market2" 
     app:showAsAction="ifRoom" 
     android:title="فروشگاه" /> 

</menu> 

这是XML代码:

` <RelativeLayout 
     android:id="@+id/activity_main" 
     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:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="com.truiton.bottomnavigation.MainActivity"> 

     <FrameLayout 
      android:id="@+id/frame_layout" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_above="@+id/navigation" 
      android:animateLayoutChanges="true"> 

     </FrameLayout> 

     <android.support.design.widget.BottomNavigationView 
      android:id="@+id/navigation" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:background="@color/colorPrimary" 
      app:itemIconTint="#fff" 
      app:itemTextColor="#F2F2F4" 
      app:menu="@menu/bottom_navigation_items"/> 
    </RelativeLayout>` 

我怎样才能解决这个问题呢?

回答

0

两个行为(而不是问题)是默认行为。如果我通过“自由空间”正确理解了你的话,这个空间就是通过移动动画来创建的,这是一个行为BottomNavigationView。 您可以覆盖onLayout方法BottomNavigationView类然后您可以使用扩展标记来避免这两个。如果需要,此方法还可为BottomNavigationView设置自定义字体系列。

public final class ExtendedBottomNavigationView extends BottomNavigationView{ 
    private final Context context; 
    private Typeface fontFace = null; 

    public ExtendedBottomNavigationView(Context context, AttributeSet attrs){ 
     super(context, attrs); 
     this.context = context; 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom){ 
     super.onLayout(changed, left, top, right, bottom); 
     final ViewGroup bottomMenu = (ViewGroup)getChildAt(0); 
     final int bottomMenuChildCount = bottomMenu.getChildCount(); 
     BottomNavigationItemView item; 
     View itemTitle; 
     Field shiftingMode; 

     if(fontFace == null){ 
      fontFace = Typeface.createFromAsset(context.getAssets(), context.getString(R.string.VazirBold)); 
     } 
     try { 
      //if you want to disable shiftingMode: 
      //shiftingMode is a private member variable so you have to get access to it like this: 
      shiftingMode = bottomMenu.getClass().getDeclaredField("mShiftingMode"); 
      shiftingMode.setAccessible(true); 
      shiftingMode.setBoolean(bottomMenu, false); 
      shiftingMode.setAccessible(false); 
     } catch (NoSuchFieldException e){ 
      e.printStackTrace(); 
     } catch (IllegalAccessException e){e.printStackTrace();} 
     //for changing font face of item titles. 
     for(int i=0; i<bottomMenuChildCount; i++){ 
      item = (BottomNavigationItemView)bottomMenu.getChildAt(i); 
      //this shows all titles of items 
      item.setChecked(true); 
      //every BottomNavigationItemView has two children, first is an itemIcon and second is an itemTitle 
      itemTitle = item.getChildAt(1); 
      //every itemTitle has two children, first is a smallLabel and second is a largeLabel. these two are type of AppCompatTextView 
      ((TextView)((BaselineLayout) itemTitle).getChildAt(0)).setTypeface(fontFace, Typeface.BOLD); 
      ((TextView)((BaselineLayout) itemTitle).getChildAt(1)).setTypeface(fontFace, Typeface.BOLD); 
     } 
    } 
} 

然后使用它是这样的:

<your.package.name.ExtendedBottomNavigationView android:id="@id/bottomMenu" style="@style/bottomMenu"/>