Align Center Menu Item text in android

后端 未结 3 761
轮回少年
轮回少年 2020-12-17 16:35

Using following code



        
相关标签:
3条回答
  • 2020-12-17 16:44

    Another way to achieve this is via setting theme

     <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeToolbar.NavigationViewPadding"
        app:itemTextColor="@color/inverse_color"
        app:itemIconTint="@color/inverse_color"
        app:itemBackground="@drawable/selector_navigation"
        app:headerLayout="@layout/activity_home_nav_header"
        app:menu="@menu/activity_home_drawer" />
    

    In your styles

        <style name="ThemeToolbar.NavigationViewPadding">
        <item name="listPreferredItemPaddingLeft">50dp</item>
        </style>
    

    0 讨论(0)
  • 2020-12-17 16:49

    I used @Mahm00d's way, that worked perfectly, but I found another way to solve the problem. It is to add \t before the String. If the space isn't enough, I can add more.

    Because Chinese words are one by one, the length of the item is same.

    0 讨论(0)
  • 2020-12-17 17:00

    You can center-align the menu items dynamically using SpannableString in your activity:

    int positionOfMenuItem = 0; //or any other postion
    MenuItem item = menu.getItem(positionOfMenuItem);
    SpannableString s = new SpannableString(settingsItemTitle);
    
    s.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_CENTER), 0, s.length(), 0);
    
    item.setTitle(s);
    
    0 讨论(0)
提交回复
热议问题