2017-04-13 61 views
1

我在我的应用程序中使用了BottomNavigationView,但菜单项的文本与小设备中的菜单图标重叠,如下面的屏幕截图所示。BottomNavigationView文字重叠在图标上

enter image description here

编辑: 这里是Java代码:

public class MainActivity extends AppCompatActivity { 
    private static final String SELECTED_ITEM = "arg_selected_item"; 

    private BottomNavigationView mBottomNav; 
    private int mSelectedItem; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mBottomNav = (BottomNavigationView) findViewById(R.id.navigation); 
     BottomNavigationViewHelper.disableShiftMode(mBottomNav); 
     mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { 
      @Override 
      public boolean onNavigationItemSelected(@NonNull MenuItem item) { 
       selectFragment(item); 
       return true; 
      } 
     }); 

     MenuItem selectedItem; 
     if (savedInstanceState != null) { 
      mSelectedItem = savedInstanceState.getInt(SELECTED_ITEM, 0); 
      selectedItem = mBottomNav.getMenu().findItem(mSelectedItem); 
     } else { 
      selectedItem = mBottomNav.getMenu().getItem(0); 
     } 
     selectFragment(selectedItem); 
    } 

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     outState.putInt(SELECTED_ITEM, mSelectedItem); 
     super.onSaveInstanceState(outState); 
    } 

    @Override 
    public void onBackPressed() { 
     MenuItem homeItem = mBottomNav.getMenu().getItem(0); 
     if (mSelectedItem != homeItem.getItemId()) { 
      // select home item 
      selectFragment(homeItem); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    private void selectFragment(MenuItem item) { 
     // init corresponding fragment 
     switch (item.getItemId()) { 
      case R.id.menu_explore: 
       pushFragment(new ExploreFragment()); 
       break; 

      case R.id.menu_how_it_works: 
       pushFragment(new HowItWorksFragment()); 
       break; 

      case R.id.menu_contact_us: 
       pushFragment(new ContactUsFragment()); 
       break; 

      case R.id.menu_profile: 
       pushFragment(new MyProfileFragment()); 
       break; 
     } 

     // update selected item 
     mSelectedItem = item.getItemId(); 

     // uncheck the other items. 
     for (int i = 0; i < mBottomNav.getMenu().size(); i++) { 
      MenuItem menuItem = mBottomNav.getMenu().getItem(i); 
      menuItem.setChecked(menuItem.getItemId() == item.getItemId()); 
     } 

     updateToolbarText(item.getTitle()); 

    } 

    protected void pushFragment(Fragment fragment) { 
     if (fragment == null) 
      return; 

     FragmentManager fragmentManager = getFragmentManager(); 
     if (fragmentManager != null) { 
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
      if (ft != null) { 
       ft.replace(R.id.container, fragment); 
       ft.commit(); 
      } 
     } 
    } 

    private void updateToolbarText(CharSequence text) { 
     ActionBar actionBar = getSupportActionBar(); 
     if (actionBar != null) { 
      actionBar.setTitle(text); 
     } 
    } 
} 

这里是XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:design="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" /> 

    <android.support.design.widget.BottomNavigationView 
     android:id="@+id/navigation" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="start" 
     android:background="@android:color/white" 
     design:menu="@menu/bottom_nav_items" /> 
</LinearLayout> 

如何解决这个问题?

请帮忙!!

+0

发布您的代码... –

+0

@ AdityaVyas-Lakhan请检查更新。 – Android

+0

XML怎么样? – Damien

回答

3

请勿在底栏上使用长文字。它超出了准则。 Bottom Bar不是像这样使用开发的。

文本标签提供短期,有意义的定义,以底部 导航图标。避免使用长文本标签,因为这些标签不会截断或换行。

请阅读Source的所有文档和用法说明。