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"/>